Java Program to demonstrate multilevel inheritance
Program
class GrandParent
{
GrandParent()
{
System.out.println("This is class 'GrandParent'");
}
void show()
{
System.out.println("This is a method in GrandParent");
}
}
class Parent extends GrandParent
{
Parent()
{
System.out.println("This is class 'Parent'");
}
void fun()
{
System.out.println("This is a method in Parent");
}
}
class Child extends Parent
{
Child()
{
System.out.println("This is class 'Child'");
}
}
public class MultiLevelInheritance
{
public static void main(String[] args)
{
Child chObj = new Child();
chObj.show();
chObj.fun();
}
}
This program demonstrates multilevel inheritance in Java.
- Multilevel inheritance is when a class inherits from another class, which in turn inherits from another class.
- This creates a chain of inheritance. For example:
- Class
Childinherits fromParent, andParentinherits fromGrandParent.
- Class
-
Class
GrandParent:- Contains:
- A constructor: Prints
"This is class 'GrandParent'"when an object of this class (or its subclass) is created. - A method
show(): Prints"This is a method in GrandParent"when called.
- A constructor: Prints
- This is the base class in the inheritance hierarchy.
- Contains:
-
Class
Parent:- Inherits from
GrandParentusing theextendskeyword. - Contains:
- A constructor: Prints
"This is class 'Parent'"when an object of this class (or its subclass) is created. - A method
fun(): Prints"This is a method in Parent"when called.
- A constructor: Prints
- As a subclass of
GrandParent, it inherits all of its methods (show()in this case).
- Inherits from
-
Class
Child:- Inherits from
Parent, which means it indirectly inherits fromGrandParentas well. - Contains:
- A constructor: Prints
"This is class 'Child'"when an object of this class is created.
- A constructor: Prints
- As a subclass of
Parent, it inherits all methods from bothParent(fun()) andGrandParent(show()).
- Inherits from
-
Class
MultiLevelInheritance:- Contains the
main()method, which is the entry point of the program. - Steps:
- Creates an instance of
Child. - The
Childconstructor is called first, which triggers the constructors of the parent classes in the hierarchy (ParentandGrandParent). - Calls the
show()method (inherited fromGrandParent) and thefun()method (inherited fromParent).
- Creates an instance of
- Contains the
Order of Constructor Execution:
-
When an object of
Childis created, the constructors are executed in the order of inheritance:GrandParentConstructor: Executes first becauseParentextends it.ParentConstructor: Executes next becauseChildextends it.ChildConstructor: Executes last.
-
The constructors are executed in the order of inheritance (
GrandParent→Parent→Child). -
The
show()method is called, which belongs toGrandParent. -
The
fun()method is called, which belongs toParent.
-
Inheritance in Java:
- The
extendskeyword is used to inherit from a parent class. - Subclasses inherit all the non-private methods and fields of their parent classes.
- The
-
Multilevel Inheritance:
Childinherits fromParent, which in turn inherits fromGrandParent.- The
Childclass can access methods from bothParentandGrandParent.
-
Constructor Chaining in Inheritance:
- In a class hierarchy, constructors are called in the order of inheritance, starting with the top-most parent class.
-
Method Inheritance:
- The
Childclass can directly call methods (show()andfun()) from its parent and grandparent classes without redefining them.
- The
Output
This is class 'GrandParent'
This is class 'Parent'
This is class 'Child'
This is a method in GrandParent
This is a method in Parent