OOP AND ENCAPSULATION
Constructor Chaining and super()
Rule: If a constructor does not explicitly call super()
or this()
, the compiler inserts super()
only if the superclass has a no-arg constructor.
class Ancestor {
Ancestor(String msg) {
System.out.println("Ancestor: " + msg);
}
// No no-arg constructor available!
}
class Parent extends Ancestor {
// β This would cause compile error:
// Parent() {} // Implicit super() call fails
// β
Must explicitly call super with argument:
Parent() {
super("Default parent message"); // Explicit call required
}
Parent(String name) {
super("Parent: " + name); // Explicit call required
}
}
class Child extends Parent {
Child() {
// β
Implicit super() works - Parent has no-arg constructor
System.out.println("Child constructor");
}
}
Constructor execution order:
Child child = new Child();
// Output:
// Ancestor: Default parent message
// Child constructor
π‘ Learning Tip: βNo free lunchβ - if parent needs arguments, children must provide them explicitly.