When an object of a subclass is created, the constructors of the superclasses are called first, starting from the top of the inheritance hierarchy down to the subclass.
So if the inheritance is like GrandParent > Parent > Son > Grandson.
So when the object of Grandson class is created, order of Constructor invoking would be: Grandparent > Parent > Son > Grandson
class Animal {
Animal() {
System.out.println("Animal constructor called");
}
}
class Dog extends Animal {
Dog() {
// Implicit call to super()
System.out.println("Dog constructor called");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog(); // Output: Animal constructor called
// Dog constructor called
}
}
The super()
keyword is used to explicitly call the superclass’ constructor. If super()
is not called explicitly, Java will automatically insert a call to the no-argument constructor of the superclass at the beginning of the subclass constructor. The call to super()
must be the first statement in the constructor and you can only call super()
once.
class Animal {
Animal() {
System.out.println("Animal no-arg constructor called");
}
Animal(String name) {
System.out.println("Animal constructor called with name: " + name);
}
}
class Dog extends Animal {
Dog() {
super("Buddy"); // Explicit call to superclass constructor with argument
System.out.println("Dog constructor called");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog(); // Output: Animal constructor called with name: Buddy
// Dog constructor called
}
}
No-Argument Constructor Requirement
If a class does not explicitly define any constructors, the Java compiler automatically provides a no-argument constructor. However, if any constructors are explicitly defined, the compiler does not provide a default no-argument constructor. This can lead to compilation errors if a superclass does not have a no-argument constructor and the subclass constructor does not explicitly call a superclass constructor.
class Animal {
Animal(String name) {
System.out.println("Animal constructor called with name: " + name);
}
}
class Dog extends Animal {
Dog() {
// super() // Compilation error: no suitable constructor found
System.out.println("Dog constructor called");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog(); // Compilation error
}
}
Constructor Chaining with this()
: In addition to using super()
, you can use this()
to call another constructor in the same class. this()
must also be the first statement in the constructor. You cannot use both this()
and super()
in the same constructor because both must be the first statement.
class Animal {
Animal() {
System.out.println("Animal no-arg constructor");
}
Animal(String name) {
System.out.println("Animal constructor with name: " + name);
}
}
class Dog extends Animal {
Dog() {
this("Buddy");
System.out.println("Dog no-arg constructor");
}
Dog(String name) {
super(name);
System.out.println("Dog constructor with name: " + name);
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog(); // Output:
// Animal constructor with name: Buddy
// Dog constructor with name: Buddy
// Dog no-arg constructor
}
}
Private Constructors
A class can have a private constructor to prevent direct instantiation. This is often used in singleton patterns. However, a subclass cannot directly invoke a private constructor of its superclass.