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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With