3
public interface A {
    class Aclass {
        int constants = 100;
        public void display()
        {
            System.out.println("Inside A");
        }
    }
    public void display();

}
public interface B {
    class Bclass {
        int constants = 130;

        public void display() {
            System.out.println("Inside B");
        }
    }
    public void display();
}

public class MultipleInheritance implements A, B {

    @Override
    public void display() {
        A.Aclass a = new A.Aclass();
        System.out.println(a.constants);
        B.Bclass b = new B.Bclass();
        System.out.println(b.constants);
    }

    public static void main(String args[]) {

        new MultipleInheritance().display();
    }
}

though it's through interface and not through a concrete class in context to which you are not inheriting anything but still is it not a code reuse even though maintaining a inner classes will be difficult but still it acts as a multiple-inheritance please clearify with memory representation if possible.

Prashant
  • 419
  • 6
  • 14
  • 5
    Nothing's multiply-inheriting there. So it's not. – Mat Sep 05 '13 at 08:30
  • but an interface is still like an abstract class with everything as final static(just syntax is different but behavior is same).please this can be a very hot question with an interviewer. – Prashant Sep 05 '13 at 08:38
  • 1
    You might want to check out the ["Abstract Classes versus Interfaces"](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html) and ["Interfaces and Multiple Inheritance"](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html) sections in the Java documentation. – Ben Sep 05 '13 at 08:42
  • ok we have just declarations to tell a class what should it conforms while implementing methods of an interface and which is still be with an abstract class having only method declarations. – Prashant Sep 05 '13 at 08:46

4 Answers4

5

Let's review what you actually have in your code. Here you declare interface A and a nested class AClass:

public interface A {
    class Aclass {
        int constants = 100;
        public void display()
        {
            System.out.println("Inside A");
        }
    }
    public void display();
}

Here you declare interface B and a nested class Bclass:

public interface B {
    class Bclass {
        int constants = 130;

        public void display() {
            System.out.println("Inside B");
        }
    }
    public void display();
}

Here you instantiate Aclass:

A.Aclass a = new A.Aclass();

And here you instantiate Bclass:

B.Bclass b = new B.Bclass();

Now note that Aclass and Bclass are two completely unrelated classes. The only common supertype these two share is Object.

Clearly, there can be no talk of multiple inheritance in a case where you don't even attempt to inherit from two types.

Here you do at least attempt to involve two supertypes:

public class MultipleInheritance implements A, B { ... }

but you never involve this type in your code, it's just a container of the main method. This type, while implementing two interfaces, does not multiply inherit anything from them: it inherits two distinct types (Aclass, Bclass). Note also that, even if they had the same name, there would still be no multiple inheritance. Only a naming clash would occur.

Multiple inheritance is strictly about the inheritance of method implementations and clearly you cannot achieve that in Java.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

java doesn't support multiple inheritance for classes. But you can implements several interfaces and that is not consider as multiple inheritance.

Follow this link too. Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

Community
  • 1
  • 1
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

This is not multiple inheritance, but this is the simulation of the same. The reason behind not allowing Multiple Inheritance in java was the Diamond problem, which does not occur in case of Interfaces see this for more info

ankit
  • 4,919
  • 7
  • 38
  • 63
0

This is not Multiple inheritance as you are not calling an instance of the subclass through the super class as shown below:

A a = new MultipleInheritance(); 
a.display();

Even if you do that, there is no confusion as the compiler looks for the type of the variable, which is A and not MulipleInheritance to see the validity of the call.

The reason the compiler is not complaining for having two display() methods is because it knows it doesn't matter which one it uses(the one from A or B) as they are both empty. This avoids the confusion that might result had both A and B bean classes as in C++.

Andromeda
  • 1,370
  • 2
  • 10
  • 15