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 from Parent, and Parent inherits from GrandParent.
  1. 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.
    • This is the base class in the inheritance hierarchy.
  2. Class Parent:

    • Inherits from GrandParent using the extends 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.
    • As a subclass of GrandParent, it inherits all of its methods (show() in this case).
  3. Class Child:

    • Inherits from Parent, which means it indirectly inherits from GrandParent as well.
    • Contains:
      • A constructor: Prints "This is class 'Child'" when an object of this class is created.
    • As a subclass of Parent, it inherits all methods from both Parent (fun()) and GrandParent (show()).
  4. 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 and GrandParent).
      • Calls the show() method (inherited from GrandParent) and the fun() method (inherited from Parent).

Order of Constructor Execution:

  • When an object of Child is created, the constructors are executed in the order of inheritance:

    1. GrandParent Constructor: Executes first because Parent extends it.
    2. Parent Constructor: Executes next because Child extends it.
    3. Child Constructor: Executes last.
  • The constructors are executed in the order of inheritance (GrandParentParentChild).

  • The show() method is called, which belongs to GrandParent.

  • The fun() method is called, which belongs to Parent.

  1. 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.
  2. Multilevel Inheritance:

    • Child inherits from Parent, which in turn inherits from GrandParent.
    • The Child class can access methods from both Parent and GrandParent.
  3. Constructor Chaining in Inheritance:

    • In a class hierarchy, constructors are called in the order of inheritance, starting with the top-most parent class.
  4. Method Inheritance:

    • The Child class can directly call methods (show() and fun()) from its parent and grandparent classes without redefining them.

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