Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
48 views7 pages

Rolee of Constructor in Inheritance

In Java, constructors are not inherited by subclasses, but the parent class constructor is automatically called when a subclass object is created to ensure proper initialization. The 'super()' keyword is used in the child class constructor to invoke the parent class constructor, and it must be the first statement. Constructor chaining refers to the sequence of constructor calls in multi-level inheritance, where the grandparent constructor runs first, followed by the parent and then the child constructor.

Uploaded by

Anand Vk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views7 pages

Rolee of Constructor in Inheritance

In Java, constructors are not inherited by subclasses, but the parent class constructor is automatically called when a subclass object is created to ensure proper initialization. The 'super()' keyword is used in the child class constructor to invoke the parent class constructor, and it must be the first statement. Constructor chaining refers to the sequence of constructor calls in multi-level inheritance, where the grandparent constructor runs first, followed by the parent and then the child constructor.

Uploaded by

Anand Vk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

🌟 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

You might also like