Saturday, March 4, 2017

Multilevel Inheritance Java

Tags

In Multilevel Inheritance the Subclass of one superclass is acting as a Superclass for another subclass. Hence the members of both superclass are present in the subclass.

Following code will illustrate the concept:

Code::
import java.util.*;
class A
{
    int a;
}
class B extends A
{
    int b;
}
class C extends B
{
    int c;
    void show()
    {
        System.out.println(a+b+c);                //a,b are members of superclass
    }
}
public class Multilevel
{

    public static void main(String[] nt)
    {   C ob=new C();
        Scanner in =new Scanner(System.in);
        System.out.print("Enter First Number:");
        ob.a=in.nextInt();
        System.out.print("Enter Second Number:");
        ob.b=in.nextInt();
        System.out.print("Enter Third Number:");
        ob.c=in.nextInt();
        System.out.print("Sum is: ");
        ob.show();
    }
   
}

Output::
Java Does not Support Multiple Inheritance Because in Multiple Inheritance if Two Classes have same members then its an ambiguity for Subclass to use which member.  So There is no Multiple Inheritance in Java. But we can achieve this with the help of Interfaces.