Multi Level and Multiple Inheritance in Java
In this class, We discuss Multi Level and Multiple Inheritance in Java.
The reader should have prior knowledge of inheritance in Java. Click Here.
We take an example and understand multi-level inheritance.
Example:
class A
{
int p;
void m1()
{
System.out.println(“First class “);
}
}
Class B extends A
{
int q=20;
void m2()
{
System.out.println(“second class”);
m1();
}
}
Class C extends B
{
int k =40;
void m5()
{
System.out.println(p);
m1();
}
}
More than one level of inheritance, we call multi-level inheritance.
Class C can use any of the members from class A or B.
class test
{
public static void main(string args[])
{
C ob = new C();
System.out.println(ob.p);
}
}
We create the object for class C.
Memory is allocated for all the instance variables in classes A, B, and C.
Multiple inheritance:
Java does not support multiple inheritances using classes.
One should understand the concept of multiple inheritance.
We have a class C which inherits classes A and B.
Class C extends A, B
One class can be inherited. Multiple classes can not be inherited in Java.