Empty constructor without calling super()

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

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

Author: Maniero, 2016-01-15

2 answers

It is always necessary to initialize the instance with the construction of the object integrally, that is, the part of the upper class needs to be constructed, and so it has to call the 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 of them is when the parent class does not have a default constructor.

Specifically this point is independent of being Beans or not, Beans has other requirement.

 4
Author: Maniero, 2019-11-20 16:52:25

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

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

What confuses a little anyone 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 constructor of the superclass without parameters.

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

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

 6
Author: utluiz, 2016-01-18 14:11:01