🌟 Role of Constructor in Inheritance
• In Java, constructors are not inherited by subclasses.
• But when you create an object of the subclass, the constructor of the parent
(super) class is called first automatically.
• This is necessary to initialize the parent class parts of the object properly before
initializing the child class parts.
Important Points:
• The parent class constructor is always called first (even if you don't write
anything).
• If you don't explicitly call a constructor of the parent class using super(), Java
automatically adds super() at the start of the child class constructor.
• You can also call a parameterized constructor of the parent class using
super(arguments).
Simple Example: Constructor in Inheritance
What is super() in Java?
super() is a special keyword used inside a child class constructor to call the
parent class constructor.
• It must be the first statement inside the child class constructor.
• It is used to ensure that the parent class part of the object gets initialized first.
• If you don't write super(), Java automatically adds a call to the no-argument
constructor of the parent class.
📘 Example with Parameterized
Constructor using super()
🌟 What is Constructor Chaining?
Constructor Chaining means calling one constructor from another constructor.
In multi-level inheritance, when we create a child object:
• First grandparent constructor runs,
• then parent constructor runs,
• and finally the child constructor runs.
This chaining happens using super() calls automatically or explicitly.
Example: Multi-Level Inheritance with Parameterized Constructors