Multilevel Inheritance Example

This program demonstrates multilevel inheritance.

Problem Statement

Write a Java program to show multilevel inheritance among Person, Student, and Graduate.

Source Code

1 class Person {
2 String name;
3 }
4 class Student extends Person {
5 int age;
6 }
7 class Graduate extends Student {
8 String degree;
9 }
10 public class MultilevelInheritance {
11 public static void main(String[] args) {
12 System.out.println("Eduinq Multilevel Inheritance");
13 Graduate g=new Graduate();
14 g.name="Arun"; g.age=22; g.degree="MCA";
15 System.out.println("Name: "+g.name+", Age: "+g.age+", Degree: "+g.degree);
16 }
17 }

Program Output

Eduinq Multilevel Inheritance
Name: Arun, Age: 22, Degree: MCA

Explanation

Multilevel inheritance chains classes. - Graduate inherits from Student, which inherits from Person. - This creates a hierarchy where attributes flow down the chain. Conclusion: Shows how to implement multilevel inheritance.

Quick Links to Explore