In multilevel inheritance, a subclass inherits from an intermediate parent class, which itself inherits from a grandparent class. For example, a Maruti 800 class inherits from a Maruti class, which inherits from a Car class, allowing the Maruti 800 class to access methods from both the Maruti class and the Car class. This is demonstrated with an example program defining Car, Maruti, and Maruti800 classes in a multilevel inheritance structure, where Maruti800 inherits properties and methods from both the Maruti and Car classes.
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
154 views
Multilevel Inheritance in Java With Example
In multilevel inheritance, a subclass inherits from an intermediate parent class, which itself inherits from a grandparent class. For example, a Maruti 800 class inherits from a Maruti class, which inherits from a Car class, allowing the Maruti 800 class to access methods from both the Maruti class and the Car class. This is demonstrated with an example program defining Car, Maruti, and Maruti800 classes in a multilevel inheritance structure, where Maruti800 inherits properties and methods from both the Maruti and Car classes.
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2
Multilevel inheritance in java with example
We discussed a bit about Multilevel inheritance in types of inheritance in java. In
this tutorial we will explain multilevel inheritance with the help of diagram and example program. Its clear with the diagram that in Multilevel inheritance there is a concept of grand parent class. If we take the example of above diagram then class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B. So in this case class C is implicitly inheriting the properties and method of class A along with B thats what is called multilevel inheritance. Example Program: In this example we have three classes Car, Maruti and Maruti800. We have done a setup class Maruti extends Car and class Maurit800 extends Maurti. With the help of this Multilevel hierarchy setup our Maurti800 class is able to use the methods of both the classes (Car and Maruti). class Car{ public Car() { System.out.println("Class Car"); } public void vehicleType() { System.out.println("Vehicle Type: Car"); } } class Maruti extends Car{ public Maruti() { System.out.println("Class Maruti"); } public void brand() { System.out.println("Brand: Maruti"); } public void speed() { System.out.println("Max: 90Kmph"); } } public class Maruti800 extends Maruti{ public Maruti800() { System.out.println("Maruti Model: 800"); } public void speed() { System.out.println("Max: 80Kmph"); } public static void main(String args[]) { Maruti800 obj=new Maruti800(); obj.vehicleType(); obj.brand();
obj.speed(); } } Output: Class Car Class Maruti Maruti Model: 800 Vehicle Type: Car Brand: Maruti Max: 80Kmph