Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Multilevel inheritance in Java language

Posted on February 2, 2017December 27, 2019
Multilevel inheritance in Java language
In this tutorial, we will discuss the subject of Multilevel inheritance in Java language

Already, we have looked at multilevel inheritance in  Inheritance in Java. Multilevel inheritance is one a concept in Java inheritance.  A base class is inherited by a intermediate class and this derived class is inherited by a child class.

In the diagram below, class C inherits class B and class B inherits class A. This means class C is the child of class B and class B is the child to class A.

Multilevel inheritance in java language
Multilevel inheritance

 

Multilevel inheritance in java language
Explanation
In the diagram above, we can clearly observe multilevel inheritance. We have three classes: principal, teacher and student. The principal is the base class, the teacher is the intermediate class and the student is the derived class.  Class teacher extends from class principal and class student extends from class teacher. Now, we can access the data members and methods of the other two classes from the class student.

This is an example of multilevel inheritance. We have three classes in this example: student, teacher and principal. Teacher extends to students class and principal extends to teacher class. The principal class is able to use the methods of both student and teacher classes.

Example 1

 

 

Example 2


class students{            //class students is the parent class

int marks=78;

}class teacher extends students{            //class teacher is the inter mediate class
float salary=40000;
}
class principal extends teacher{   // class principal is the child class
float bonus=10000;

public static void main (String args[]){  //main method

principal p=new principal();   //create object

System.out.println(“teacher salary is:”+p.salary);  
//access variable of teacher class from this class
System.out.println(“principal bonus is:”+p.bonus);
//access variable of this class
System.out.println(“principal marks is:”+p.marks);
//access variable of this class
}

}

 

Related Links
 
Inheritance in Java                            Inheritance in C++
 
Single level inheritance in java          Single level inheritance in C++
 
Hierarchical Inheritance in Java       Multiple Inheritance in C++
 
Interface in Java
Hybrid inheritance in C++

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes