Constructor method in inheritance in java:
In Java, when a class is inherited (i.e., when a subclass extends a superclass), the constructor
of the superclass is not inherited by the subclass, but it is called when an object of the
subclass is created.
This ensures that the superclass is properly initialized before the subclass-specific attributes
and methods are executed.
Key Points:
1. Calling Superclass Constructor (super())
o The constructor of the superclass is called automatically before the subclass
constructor executes.
o If the superclass has a parameterized constructor, the subclass must
explicitly call it using super(arguments).
2. Default Constructor in Inheritance
o If no constructor is defined in the superclass, Java provides a default
constructor (super()), which is implicitly called.
3. Parameterized Constructor in Inheritance
o If the superclass has a parameterized constructor, then the subclass must
explicitly invoke it using super() because Java does not provide a default
constructor if a parameterized constructor exists.
Examples
1. Using Default Constructor
If the superclass has a default constructor, it is automatically called.
class Parent {
Parent() {
System.out.println("Parent class constructor");
}
}
class Child extends Parent {
Child() {
System.out.println("Child class constructor");
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
}
}
Output:
Parent class constructor
Child class constructor
👉 The superclass constructor (Parent()) is called before the subclass constructor (Child()).
2. Using Parameterized Constructor with super()
If the superclass has a parameterized constructor, the subclass must explicitly call it.
class Parent {
Parent(String msg) {
System.out.println("Parent constructor: " + msg);
}
}
class Child extends Parent {
Child(String msg) {
super(msg); // Call superclass constructor
System.out.println("Child constructor: " + msg);
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child("Hello!");
}
}
Output:
Parent constructor: Hello!
Child constructor: Hello!
👉 The super(msg) explicitly calls the superclass constructor with a parameter.
3. Super and Subclass with Multiple Constructors
If both classes have multiple constructors, super() must be used appropriately.
class Parent {
Parent() {
System.out.println("Parent default constructor");
}
Parent(int x) {
System.out.println("Parent parameterized constructor:
" + x);
}
}
class Child extends Parent {
Child() {
super(10); // Explicitly calling parameterized
constructor of Parent
System.out.println("Child default constructor");
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
}
}
Output:
Parent parameterized constructor: 10
Child default constructor
👉 The super(10) calls the parameterized constructor of Parent.
Key Takeaways
1. Constructors are not inherited but are called during object creation.
2. super() is used to call the superclass constructor explicitly.
3. If the superclass has a parameterized constructor, the subclass must call it
explicitly.