I know that super is called when you create a subclass in Java. But is super called in every constructor, or only in the first constructor of the subclass?
Thanks
I know that super is called when you create a subclass in Java. But is super called in every constructor, or only in the first constructor of the subclass?
Thanks
But is super called in every constructor, or only in the first constructor of the subclass?
Firstly there is nothing like first Constructor or Second Constructor , Since Constructor calling will be based on Object Creation.
if you create Object this way
new MyClass(); //then no-argument constructor will be called
and if Object Created in this way
new MyClass("ABC");// then parameterized constructor will be called
So super()
is called in each and every Constructor in the first line .
Note : if you explicitly call other constructor in the same class using this()
then in that scenario super()
will not be called in existing constructor
You can call another constructor from the same class like this:
MyClass(int someValue) { this(someValue, false); .... }
In this case the super constructor is not called, but it will be called in the first constructor which doesn't call another constructor from the class.