Empty constructor without super () call

6

When I make parameterized constructors, I create an empty constructor as well.

In the empty constructor, should I always make the super () call? Because? (Take into account, that my class is just a JavaBean.)

    
asked by anonymous 15.01.2016 / 12:54

2 answers

5

It is always necessary to initialize the instance with the object construct in full, ie the upper class needs to be built, and so you have to call super() . If this is not done explicitly in the constructor, the compiler does it for you. Even for Objects . You only need to do it manually if you have a reason. One is when the parent class does not have a default constructor.

Specifically this point is independent of whether or not Beans. Beans has other requirements.

    
15.01.2016 / 13:04
5

In Java, every class necessarily needs to call a constructor from its superclass, this being the first command inside the constructor.

Even if a class does not extend any other classes directly, then the super() of Object will be called anyway.

What puzzles a bit about who has not yet studied this concept is that when you do not explicitly call super() in a constructor, the Java compiler implicitly adds a call to the superclass constructor with no parameters.

This occurs in the same way that the compiler also adds a default constructor, without parameters, when you explicitly declare no constructor.

If the superclass does not have a parameterless constructor, then the compiler forces you to call any of the constructors using super .

    
16.01.2016 / 00:35