Explore Java Hacks and Tutorials
Be Social. Be Updated.

Multi level Inheritance in java


Java supports multi-level inheritance as C++.
class A
{
    public void printA()
    {
    System.out.println("I am in class A");
    }
}
class B extends A
{
    public void printB()
    {
    System.out.println("I am in class B");
    }
}
class C extends B
{
    public void printC()
    {
    System.out.println("I am in class C");
    }
}
public class Main
{
    public static void main(String args[])
    {
    A a=new A();
    a.printA();
    // a.printB(); error
    // a.printC(); error

    B b=new B();
    b.printA();
    b.printB();
    // b.printC(); error
   
    C c=new C();
    c.printA();
    c.printB();
    c.printC();
    }
}
class A : Internally inherits java.lang.Object
class B : Inherits class A
class C : Inherits class B
class Main : Internally inherits java.lang.Object and calls the methods in classes A,B and C.



Posted by at on

Tags: Object Oriented Programming in java