Multilevel Inheritance In Java Example
Chapter:
Inheritance
Last Updated:
22-09-2018 08:18:37 UTC
Program:
/* ............... START ............... */
class Car {
public Car() {
System.out.println("Class Car constructor");
}
public void vehicleType() {
System.out.println("Vehicle Type: Car");
}
}
class Audi extends Car {
public Audi() {
System.out.println("Class Audi");
}
public void brand() {
System.out.println("Brand: Audi");
}
public void speed() {
System.out.println("Max: 100Kmph");
}
}
class AudiQ3 extends Audi {
public AudiQ3() {
System.out.println("Audi Model: Q3");
}
public void speed() {
System.out.println("Max: 120Kmph");
}
}
public class JavaMultiLevelInheritance {
public static void main(String args[]) {
Audi obj = new Audi();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
/* ............... END ............... */
Output
Class Car constructor
Class Audi
Vehicle Type: Car
Brand: Audi
Max: 100Kmph
Notes:
-
Multiple Inheritance refers to the concept of one class extending (Or inherits) more than one base class. The inheritance we learnt earlier had the concept of one base class or parent. The problem with “multiple inheritance” is that the derived class will have to manage the dependency on two base classes.
- Multilevel inheritance refers to a mechanism in OO technology where one can inherit from a derived class, thereby making this derived class the base class for the new class.
- Most of the new OO languages like Small Talk, Java, C# do not support Multiple inheritance. Multiple Inheritance is supported in C++.
Tags
Multilevel Inheritance, Java, multilevel inheritance in java using super keyword, hierarchical inheritance in java Example