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
Child
inherits fromParent
, andParent
inherits 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
GrandParent
using theextends
keyword. - 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 fromGrandParent
as 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
Child
constructor is called first, which triggers the constructors of the parent classes in the hierarchy (Parent
andGrandParent
). - 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
Child
is created, the constructors are executed in the order of inheritance:GrandParent
Constructor: Executes first becauseParent
extends it.Parent
Constructor: Executes next becauseChild
extends it.Child
Constructor: 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
extends
keyword is used to inherit from a parent class. - Subclasses inherit all the non-private methods and fields of their parent classes.
- The
-
Multilevel Inheritance:
Child
inherits fromParent
, which in turn inherits fromGrandParent
.- The
Child
class can access methods from bothParent
andGrandParent
.
-
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
Child
class 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