What is (is there?) a purpose behind declaring a method twice when parent class appears to not change any properties?

旧街凉风 提交于 2019-12-25 01:05:50

问题


So I am looking at these respective classes (and subclasses)...

public class Control{
    public Control(){}
    public Control(String name, String type, ContainerControl owner){
        //do stuff here
    }
}


public class ContainerControl extends Control{
    public ContainerControl() { super(); }
    public ContainerControl(String name, String type, ContainerControl owner)
    {
        super(name, type, owner);
        controls = new Controls(); //exists somewhere else 
    }
}

public class SSTab extends ContainerControl{
    public SSTab(){ super(); }
    public SSTab(String name, String value, ContainerControl owner)
    {
        super(name, value, owner);
    }
}

I am confused about the purpose behind the method declarations with no parameters which use super (). From the research I have done, I would be inclined to believe that they were necessary for either overloading the methods with properties from respective parent classes, or overriding. However, as they are calling super() which to my understanding would find the parameter-less method in the super class (which once again does the same thing), I don't see what could actually be getting changed.

As a result of being part of a huge project with massive amounts of subclasses I can't just remove the first method declarations from each class and run to see what happens (to identify whether there is a purpose), and I can't seem to find a reference to explain what these initial method declarations which use super are actually doing.

Any help or links would be incredibly appreciated!


Follow up question:

Why can't SSTab just be initialized with parameters with CControl which can be initialized with parameters with Control? I don't see how the parameterless constructor variables are necessary to initialize the kinds of objects, when there are constructor variables with parameters which call super anyways.


回答1:


This is Constructor Overloading; a constructor is not a method.

I'm not sure where controls is defined, but i'm pretty sure that's a typo. In any case, Super is used to call the parent version of a method/constructor. It's always needed in a constructor for subclasses such as the example you provided. Think of it like this:

SSTab IS A ContainerControl object; so in order to make an SSTab you first have to make a ContainerControl. However, CControl IS A Control object. Therefore, you must first make a Control Object. Kind of like, you were born because your parents were born. They were born because THIER parents were born.

Follow up answer:

What I can say (based on my understanding of your question) is that this is kind of like how there are certain laws to follow. For example, in real life, if you don't have a lawyer one will be provided to you. If you call SSTab(param x, y z), and you don't call the super function (with params x y z), then the default one will be provided to you (which will create an empty CControl, and an empty control). Because you did not define inside SSTab to use the other one, it will use the default one and this is not always what you want to do




回答2:


This is an example of Constructor overloading - since those aren't really methods.

Constructor overloading in this manner allows you to create a Control object that hasn't been initialised, and a Control object that has been provided a name.

I would assume each class also exposes public methods for setting required fields, but this isn't shown in the example code in the question (understandably, IMO).

Additionally, sometimes it is useful to have parameter-less Constructors when loading objects via Reflection (although you can call a Constructor with parameters using Reflection too).



来源:https://stackoverflow.com/questions/27971219/what-is-is-there-a-purpose-behind-declaring-a-method-twice-when-parent-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!