Practical-22
Write a java program which explains the concept of Multilevel Inheritance.
Introduction
Multilevel inheritance is a type of inheritance in which a derived class is created from a base class, and the derived class in turn is used as the base class to create another class, and so on up to any level. In this type of inheritance, a lower level class acquires the properties of the higher level class and add its own properties to it.
Code
class Parent
{
void method()
{
System.out.println("Parent Class");
}
}
class Child extends Parent
{
void method()
{
System.out.println("Child Class");
}
}
class GrandChild extends Child
{
void method()
{
System.out.println("Grand Child Class");
}
public static void main(String args[])
{
GrandChild gc = new GrandChild();
gc.method();
}
}
Output
Grand Child Class
Explanation
The example above is an example of multilevel inheritance with three classes, Parent, Child and Grandchild, where Child class inherits from the Parent class and Grandchild class inherits from the Child class. This example illustrates the properties of all three classes and the output which is “Grand Child Class”. :::