Learn Multilevel inheritance in java oops with code example – In Multilevel inheritance, a class is derived from another class which is also derived from some another class … For example class C extends class B and class B extends class A then this type of inheritance in java language is known as multilevel inheritance.
Multilevel inheritance Code Example
/*
* Multilevel inheritance java program example
*/
/*
* In this program we basically performing three tier level
* of inheritance.
*/
//Class GrandFather is the base class or parent class
public class GrandFather {
void oldage() {
System.out.println("we are in grandpa class");
}
}
// Father class is the child class of class Grandfather
class Father extends GrandFather {
void Age() {
System.out.println("we are in father class");
}
}
// class Baby is the child class of class Father
class Baby extends Father {
void newage() {
System.out.println("we are in baby class");
}
public void play() {
System.out.println("Baby is Playing");
}
}
// client class with all the method calls
class TestMultiLevelInheritence {
// Display method under which different methods are called
// through the reference of the child class Baby
public static void Display() {
// creating object of the class Baby
Baby b = new Baby();
b.oldage();
b.Age();
b.newage();
b.play();
}
// main method with Display Method call...
public static void main(String[] args) {
System.out.println("Information:");
Display();
}
}
Output:
Information:
we are in grandpa class
we are in father class
we are in baby class
Baby is Playing