Published by: Dikshya
Published date: 26 Jun 2023
Constructors and inheritance are closely related concepts in object-oriented programming. When a class inherits from another class, the constructors of both the superclass and the subclass play an important role. Let's explore how constructors and inheritance interact.
Constructor in the Superclass:
When an object of a class is formed, a constructor, a special member method, is invoked. Constructors in the superclass are in charge of setting the superclass object's state to an initial value. When a subclass is created, either implicitly or explicitly, it calls the superclass's constructor to initialize the inherited members.
The subclass constructor must explicitly invoke one of the superclass constructors using the super() keyword if the superclass does not have a parameterless (default) constructor. The superclass will be correctly initialized before the subclass as a result.
Example:
public class Superclass {
private int value;
public Superclass(int value) {
this.value = value;
}
}
public class Subclass extends Superclass {
public Subclass(int value) {
super(value); // Call to superclass constructor
}
}
Constructor in Sub class:
Initializing the state of the subclass object is the responsibility of the subclass constructor. It can use super() to explicitly invoke the superclass constructor, initializing the inherited elements before doing further initialization unique to the subclass.
Similar to any other method, the subclass constructor can overload constructors to offer additional object creation options.
Example:
public class Superclass {
private int value;
public Superclass(int value) {
this.value = value;
}
}
public class Subclass extends Superclass {
private String name;
public Subclass(int value, String name) {
super(value); // Call to superclass constructor
this.name = name;
}
}
Inheritance and Constructor Chaining:
When a subclass constructor is called, it automatically invokes the appropriate superclass constructor before executing its own code. This process is known as constructor chaining. By chaining constructors, each level of the inheritance hierarchy gets an opportunity to initialize its own state.
The super()
keyword is used to explicitly call a constructor in the superclass. If super()
is not explicitly called, the compiler automatically inserts a call to the parameterless (default) constructor of the superclass. If the superclass does not have a parameterless constructor, the subclass must explicitly call a superclass constructor using super()
.
Example:
public class Superclass {
private int value;
public Superclass(int value) {
this.value = value;
}
}
public class Subclass extends Superclass {
private String name;
public Subclass(int value, String name) {
super(value); // Call to superclass constructor
this.name = name;
}
}
In the above example, when a Subclass
object is created, the Subclass
constructor first calls the Superclass
constructor using super(value)
. This ensures that the value
field of the superclass is properly initialized. Then, the name
field of the subclass is initialized.
Constructors and inheritance work together to ensure that objects in an inheritance hierarchy are properly initialized at each level. Superclass constructors take care of initializing inherited members, while subclass constructors handle their own specific initialization along with invoking the appropriate superclass constructor.