Monday, August 29, 2011

Multilevel Inheritance Example

/*In my below example class C is a subclass of class B and class B is again a subclass of class A.
This in nothing but a Multilevel Inheritance concept.
Now as a Multilevel Inheritance concept we can call class A as well as class B s' members from class C easily .*/


//A.java
public class A
{
void DisplayA()
{
System.out.println("I am in A");
}
}

//B.java
public class B extends A
{
void DisplayB()
{
System.out.println("I am in B");
}
}

//C.java
public class C extends B
{
void DisplayC()
{
System.out.println("I am in C");
}
}

//MainClass.java
public class MainClass
{
public static void main(String args[])
{
C c = new C();
c.DisplayA();
c.DisplayB();
c.DisplayC();
}
}


Output=>
I am in A
I am in B
I am in C

No comments:

Post a Comment