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.)
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.)
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.
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.
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
.