Why does Java not allow multiple inheritance but does allow conforming to multiple interfaces with default implementations
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
I am not asking this -> Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
In Java, multiple inheritance isn't allowed, but, after Java 8, implemented interfaces can have default methods, just like abstract classes. Within this context, it multiple inheritance should also be allowed.
java inheritance interface java-8 abstract
add a comment |Â
up vote
9
down vote
favorite
I am not asking this -> Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
In Java, multiple inheritance isn't allowed, but, after Java 8, implemented interfaces can have default methods, just like abstract classes. Within this context, it multiple inheritance should also be allowed.
java inheritance interface java-8 abstract
maybe you should ask those who implemented it?
– Stultuske
2 hours ago
:) i am asking it is not allowed to implement to 2 inherit 2 abstract classes. but allowed implement interface with default methods. If it allowed to this, it should also allowed to multiple inheritance.
– Asanka
2 hours ago
2
yes, but you are asking it to a community that can only guess for the motives of those who implemented it. Why is it allowed? because someone at Oracle decided so. Though I haven't used default methods (yet), I understood there 's somewhat an order in which method to use if there are several ones, but I'm not sure which one gets favoured.
– Stultuske
2 hours ago
You should make your question clearer. You're asking twice (in the title and the body) "why java not allowed to multiple inheritance but allowed to implemented to interfaces which are already implemented?" - and the duplicate that you mention yourself is a duplicate of that. Somehow you manage to mention default methods but it's unclear what your question is. Also, your comments are not part of your question - if you feel your comment clarifies your question, edit it into your question please.
– Erwin Bolwidt
2 hours ago
ok, i'll edit it. :)
– Asanka
2 hours ago
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
I am not asking this -> Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
In Java, multiple inheritance isn't allowed, but, after Java 8, implemented interfaces can have default methods, just like abstract classes. Within this context, it multiple inheritance should also be allowed.
java inheritance interface java-8 abstract
I am not asking this -> Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?
In Java, multiple inheritance isn't allowed, but, after Java 8, implemented interfaces can have default methods, just like abstract classes. Within this context, it multiple inheritance should also be allowed.
java inheritance interface java-8 abstract
java inheritance interface java-8 abstract
edited 3 mins ago
dandan78
9,60495068
9,60495068
asked 2 hours ago
Asanka
324112
324112
maybe you should ask those who implemented it?
– Stultuske
2 hours ago
:) i am asking it is not allowed to implement to 2 inherit 2 abstract classes. but allowed implement interface with default methods. If it allowed to this, it should also allowed to multiple inheritance.
– Asanka
2 hours ago
2
yes, but you are asking it to a community that can only guess for the motives of those who implemented it. Why is it allowed? because someone at Oracle decided so. Though I haven't used default methods (yet), I understood there 's somewhat an order in which method to use if there are several ones, but I'm not sure which one gets favoured.
– Stultuske
2 hours ago
You should make your question clearer. You're asking twice (in the title and the body) "why java not allowed to multiple inheritance but allowed to implemented to interfaces which are already implemented?" - and the duplicate that you mention yourself is a duplicate of that. Somehow you manage to mention default methods but it's unclear what your question is. Also, your comments are not part of your question - if you feel your comment clarifies your question, edit it into your question please.
– Erwin Bolwidt
2 hours ago
ok, i'll edit it. :)
– Asanka
2 hours ago
add a comment |Â
maybe you should ask those who implemented it?
– Stultuske
2 hours ago
:) i am asking it is not allowed to implement to 2 inherit 2 abstract classes. but allowed implement interface with default methods. If it allowed to this, it should also allowed to multiple inheritance.
– Asanka
2 hours ago
2
yes, but you are asking it to a community that can only guess for the motives of those who implemented it. Why is it allowed? because someone at Oracle decided so. Though I haven't used default methods (yet), I understood there 's somewhat an order in which method to use if there are several ones, but I'm not sure which one gets favoured.
– Stultuske
2 hours ago
You should make your question clearer. You're asking twice (in the title and the body) "why java not allowed to multiple inheritance but allowed to implemented to interfaces which are already implemented?" - and the duplicate that you mention yourself is a duplicate of that. Somehow you manage to mention default methods but it's unclear what your question is. Also, your comments are not part of your question - if you feel your comment clarifies your question, edit it into your question please.
– Erwin Bolwidt
2 hours ago
ok, i'll edit it. :)
– Asanka
2 hours ago
maybe you should ask those who implemented it?
– Stultuske
2 hours ago
maybe you should ask those who implemented it?
– Stultuske
2 hours ago
:) i am asking it is not allowed to implement to 2 inherit 2 abstract classes. but allowed implement interface with default methods. If it allowed to this, it should also allowed to multiple inheritance.
– Asanka
2 hours ago
:) i am asking it is not allowed to implement to 2 inherit 2 abstract classes. but allowed implement interface with default methods. If it allowed to this, it should also allowed to multiple inheritance.
– Asanka
2 hours ago
2
2
yes, but you are asking it to a community that can only guess for the motives of those who implemented it. Why is it allowed? because someone at Oracle decided so. Though I haven't used default methods (yet), I understood there 's somewhat an order in which method to use if there are several ones, but I'm not sure which one gets favoured.
– Stultuske
2 hours ago
yes, but you are asking it to a community that can only guess for the motives of those who implemented it. Why is it allowed? because someone at Oracle decided so. Though I haven't used default methods (yet), I understood there 's somewhat an order in which method to use if there are several ones, but I'm not sure which one gets favoured.
– Stultuske
2 hours ago
You should make your question clearer. You're asking twice (in the title and the body) "why java not allowed to multiple inheritance but allowed to implemented to interfaces which are already implemented?" - and the duplicate that you mention yourself is a duplicate of that. Somehow you manage to mention default methods but it's unclear what your question is. Also, your comments are not part of your question - if you feel your comment clarifies your question, edit it into your question please.
– Erwin Bolwidt
2 hours ago
You should make your question clearer. You're asking twice (in the title and the body) "why java not allowed to multiple inheritance but allowed to implemented to interfaces which are already implemented?" - and the duplicate that you mention yourself is a duplicate of that. Somehow you manage to mention default methods but it's unclear what your question is. Also, your comments are not part of your question - if you feel your comment clarifies your question, edit it into your question please.
– Erwin Bolwidt
2 hours ago
ok, i'll edit it. :)
– Asanka
2 hours ago
ok, i'll edit it. :)
– Asanka
2 hours ago
add a comment |Â
6 Answers
6
active
oldest
votes
up vote
8
down vote
Things are not so simple.
If a class implements multiple interfaces that defines default methods with the same signature the compiler will force you to override this method for the class.
I think that we cannot apply the same logic for multiple inheritances because multiples issues could occur which the main are :
- overriding the method of an inherited class could introduce side effects and change the overall behavior of the inherited class if the class relies on this method internally.
- how to inherit multiple fields ? And even if the language allowed it you would have exactly the same issue as this previously quoted : side effect in the behavior of the inherited class : a
int foo
field defined in aA
andB
class that you want to subclass doesn't have the same meaning and intention.
good answer! I added one too with some more details (I was on the bus when the question was raised, could not type that much), hope you don't mind.
– Eugene
12 mins ago
add a comment |Â
up vote
4
down vote
Java doesn't allow multiple inheritance for fields. This would be difficult to support in the JVM as you can only have references to the start of an object where the header is, not arbitrary memory locations.
In Oracle/Openjdk, objects have a header followed by the fields of the most super class, then the next most super class, etc. It would be a significant change to allow the fields of a class to appear at different offsets relative to the header of an object for different subclasses. Most likely object references would have to become a reference to the object header and a reference to the fields to support this.
add a comment |Â
up vote
2
down vote
default
methods in interfaces pose a problem that :
If both of the implemented interfaces define a default method with
same method signature, then the implementation class does not know
which default method to use.
The implementation class should define explicitly specify which default method to use or define it's own one.
Thus default
methods in Java-8 do not facilitate multiple inheritance. The main motivation behind default methods is that if at some point we need to add a method to an existing interface, we can add a method without changing the existing implementation classes. In this way, the interface is still compatible with older versions. However, we should remember the motivation of using Default Methods and should keep the separation of interface and implementation.
add a comment |Â
up vote
1
down vote
That is mostly related to "diamonds problem" i think. Right now if you implement multiple interfaces with the same method, compiler forces you to override method the one you want to implement, because it don't know which on to use. I guess Java creators wanted to remove this problem back when interfaces couldn't use default methods. Now they came up with idea, that is good to be able to have methods with implementation in interfaces, as you can still use those as functional interfaces in streams / lambda expressions and utilize their default methods in processing. You cannot do that with classes but diamond problem still exist there. That is my guess :)
add a comment |Â
up vote
0
down vote
Java is like a real life. You can get only your family's gen(mother and father = family) as inheritance. But you can get most of people's behaviour like your friends , your teachers or others.
add a comment |Â
up vote
0
down vote
The language designers already thought about that, so these things are enforced by the compiler. So if you define:
interface First
default void go()
interface Second
default void go()
And you implement a class for both interfaces:
static class Impl implements First, Second
you will get a compilation error; and you would need to override go
to not create the ambiguity around it.
But you could be thinking that you can trick the compiler here, by doing:
interface First
public default void go()
static abstract class Second
abstract void go();
static class Impl extends Second implements First
You could think that First::go
already provides an implementation for Second::go
and it should be fine. This is too taken care of, thus this does not compile either.
JLS 9.4.1.3 : Similarly, when an abstract and a default method with matching signatures are inherited, we produce an error. In this case, it would be possible to give priority to one or the other - perhaps we would assume that the default method provides a reasonable implementation for the abstract method, too. But this is risky, since other than the coincidental name and signature, we have no reason to believe that the default method behaves consistently with the abstract method's contract - the default method may not have even existed when the subinterface was originally developed. It is safer in this situation to ask the user to actively assert that the default implementation is appropriate (via an overriding declaration).
The last point I would bring in, to solidify that multiple inheritance is not allowed even with new additions in java, is that static methods from interfaces are not inherited. static methods are inherited by default:
static class Bug
static void printIt()
System.out.println("Bug...");
static class Spectre extends Bug
static void test()
printIt(); // this will work just fine
But if we change that for an interface (and you can implement multiple interfaces, unlike classes):
interface Bug
static void printIt()
System.out.println("Bug...");
static class Spectre implements Bug
static void test()
printIt(); // this will not compile
Now, this is prohibited by the compiler and JLS
too:
JLS 8.4.8 : A class does not inherit static methods from its superinterfaces.
add a comment |Â
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
Things are not so simple.
If a class implements multiple interfaces that defines default methods with the same signature the compiler will force you to override this method for the class.
I think that we cannot apply the same logic for multiple inheritances because multiples issues could occur which the main are :
- overriding the method of an inherited class could introduce side effects and change the overall behavior of the inherited class if the class relies on this method internally.
- how to inherit multiple fields ? And even if the language allowed it you would have exactly the same issue as this previously quoted : side effect in the behavior of the inherited class : a
int foo
field defined in aA
andB
class that you want to subclass doesn't have the same meaning and intention.
good answer! I added one too with some more details (I was on the bus when the question was raised, could not type that much), hope you don't mind.
– Eugene
12 mins ago
add a comment |Â
up vote
8
down vote
Things are not so simple.
If a class implements multiple interfaces that defines default methods with the same signature the compiler will force you to override this method for the class.
I think that we cannot apply the same logic for multiple inheritances because multiples issues could occur which the main are :
- overriding the method of an inherited class could introduce side effects and change the overall behavior of the inherited class if the class relies on this method internally.
- how to inherit multiple fields ? And even if the language allowed it you would have exactly the same issue as this previously quoted : side effect in the behavior of the inherited class : a
int foo
field defined in aA
andB
class that you want to subclass doesn't have the same meaning and intention.
good answer! I added one too with some more details (I was on the bus when the question was raised, could not type that much), hope you don't mind.
– Eugene
12 mins ago
add a comment |Â
up vote
8
down vote
up vote
8
down vote
Things are not so simple.
If a class implements multiple interfaces that defines default methods with the same signature the compiler will force you to override this method for the class.
I think that we cannot apply the same logic for multiple inheritances because multiples issues could occur which the main are :
- overriding the method of an inherited class could introduce side effects and change the overall behavior of the inherited class if the class relies on this method internally.
- how to inherit multiple fields ? And even if the language allowed it you would have exactly the same issue as this previously quoted : side effect in the behavior of the inherited class : a
int foo
field defined in aA
andB
class that you want to subclass doesn't have the same meaning and intention.
Things are not so simple.
If a class implements multiple interfaces that defines default methods with the same signature the compiler will force you to override this method for the class.
I think that we cannot apply the same logic for multiple inheritances because multiples issues could occur which the main are :
- overriding the method of an inherited class could introduce side effects and change the overall behavior of the inherited class if the class relies on this method internally.
- how to inherit multiple fields ? And even if the language allowed it you would have exactly the same issue as this previously quoted : side effect in the behavior of the inherited class : a
int foo
field defined in aA
andB
class that you want to subclass doesn't have the same meaning and intention.
edited 2 hours ago
user7
8,09032038
8,09032038
answered 2 hours ago


davidxxx
57.8k54379
57.8k54379
good answer! I added one too with some more details (I was on the bus when the question was raised, could not type that much), hope you don't mind.
– Eugene
12 mins ago
add a comment |Â
good answer! I added one too with some more details (I was on the bus when the question was raised, could not type that much), hope you don't mind.
– Eugene
12 mins ago
good answer! I added one too with some more details (I was on the bus when the question was raised, could not type that much), hope you don't mind.
– Eugene
12 mins ago
good answer! I added one too with some more details (I was on the bus when the question was raised, could not type that much), hope you don't mind.
– Eugene
12 mins ago
add a comment |Â
up vote
4
down vote
Java doesn't allow multiple inheritance for fields. This would be difficult to support in the JVM as you can only have references to the start of an object where the header is, not arbitrary memory locations.
In Oracle/Openjdk, objects have a header followed by the fields of the most super class, then the next most super class, etc. It would be a significant change to allow the fields of a class to appear at different offsets relative to the header of an object for different subclasses. Most likely object references would have to become a reference to the object header and a reference to the fields to support this.
add a comment |Â
up vote
4
down vote
Java doesn't allow multiple inheritance for fields. This would be difficult to support in the JVM as you can only have references to the start of an object where the header is, not arbitrary memory locations.
In Oracle/Openjdk, objects have a header followed by the fields of the most super class, then the next most super class, etc. It would be a significant change to allow the fields of a class to appear at different offsets relative to the header of an object for different subclasses. Most likely object references would have to become a reference to the object header and a reference to the fields to support this.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Java doesn't allow multiple inheritance for fields. This would be difficult to support in the JVM as you can only have references to the start of an object where the header is, not arbitrary memory locations.
In Oracle/Openjdk, objects have a header followed by the fields of the most super class, then the next most super class, etc. It would be a significant change to allow the fields of a class to appear at different offsets relative to the header of an object for different subclasses. Most likely object references would have to become a reference to the object header and a reference to the fields to support this.
Java doesn't allow multiple inheritance for fields. This would be difficult to support in the JVM as you can only have references to the start of an object where the header is, not arbitrary memory locations.
In Oracle/Openjdk, objects have a header followed by the fields of the most super class, then the next most super class, etc. It would be a significant change to allow the fields of a class to appear at different offsets relative to the header of an object for different subclasses. Most likely object references would have to become a reference to the object header and a reference to the fields to support this.
answered 2 hours ago
Peter Lawrey
430k55539921
430k55539921
add a comment |Â
add a comment |Â
up vote
2
down vote
default
methods in interfaces pose a problem that :
If both of the implemented interfaces define a default method with
same method signature, then the implementation class does not know
which default method to use.
The implementation class should define explicitly specify which default method to use or define it's own one.
Thus default
methods in Java-8 do not facilitate multiple inheritance. The main motivation behind default methods is that if at some point we need to add a method to an existing interface, we can add a method without changing the existing implementation classes. In this way, the interface is still compatible with older versions. However, we should remember the motivation of using Default Methods and should keep the separation of interface and implementation.
add a comment |Â
up vote
2
down vote
default
methods in interfaces pose a problem that :
If both of the implemented interfaces define a default method with
same method signature, then the implementation class does not know
which default method to use.
The implementation class should define explicitly specify which default method to use or define it's own one.
Thus default
methods in Java-8 do not facilitate multiple inheritance. The main motivation behind default methods is that if at some point we need to add a method to an existing interface, we can add a method without changing the existing implementation classes. In this way, the interface is still compatible with older versions. However, we should remember the motivation of using Default Methods and should keep the separation of interface and implementation.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
default
methods in interfaces pose a problem that :
If both of the implemented interfaces define a default method with
same method signature, then the implementation class does not know
which default method to use.
The implementation class should define explicitly specify which default method to use or define it's own one.
Thus default
methods in Java-8 do not facilitate multiple inheritance. The main motivation behind default methods is that if at some point we need to add a method to an existing interface, we can add a method without changing the existing implementation classes. In this way, the interface is still compatible with older versions. However, we should remember the motivation of using Default Methods and should keep the separation of interface and implementation.
default
methods in interfaces pose a problem that :
If both of the implemented interfaces define a default method with
same method signature, then the implementation class does not know
which default method to use.
The implementation class should define explicitly specify which default method to use or define it's own one.
Thus default
methods in Java-8 do not facilitate multiple inheritance. The main motivation behind default methods is that if at some point we need to add a method to an existing interface, we can add a method without changing the existing implementation classes. In this way, the interface is still compatible with older versions. However, we should remember the motivation of using Default Methods and should keep the separation of interface and implementation.
answered 2 hours ago
S.K.
1,932717
1,932717
add a comment |Â
add a comment |Â
up vote
1
down vote
That is mostly related to "diamonds problem" i think. Right now if you implement multiple interfaces with the same method, compiler forces you to override method the one you want to implement, because it don't know which on to use. I guess Java creators wanted to remove this problem back when interfaces couldn't use default methods. Now they came up with idea, that is good to be able to have methods with implementation in interfaces, as you can still use those as functional interfaces in streams / lambda expressions and utilize their default methods in processing. You cannot do that with classes but diamond problem still exist there. That is my guess :)
add a comment |Â
up vote
1
down vote
That is mostly related to "diamonds problem" i think. Right now if you implement multiple interfaces with the same method, compiler forces you to override method the one you want to implement, because it don't know which on to use. I guess Java creators wanted to remove this problem back when interfaces couldn't use default methods. Now they came up with idea, that is good to be able to have methods with implementation in interfaces, as you can still use those as functional interfaces in streams / lambda expressions and utilize their default methods in processing. You cannot do that with classes but diamond problem still exist there. That is my guess :)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
That is mostly related to "diamonds problem" i think. Right now if you implement multiple interfaces with the same method, compiler forces you to override method the one you want to implement, because it don't know which on to use. I guess Java creators wanted to remove this problem back when interfaces couldn't use default methods. Now they came up with idea, that is good to be able to have methods with implementation in interfaces, as you can still use those as functional interfaces in streams / lambda expressions and utilize their default methods in processing. You cannot do that with classes but diamond problem still exist there. That is my guess :)
That is mostly related to "diamonds problem" i think. Right now if you implement multiple interfaces with the same method, compiler forces you to override method the one you want to implement, because it don't know which on to use. I guess Java creators wanted to remove this problem back when interfaces couldn't use default methods. Now they came up with idea, that is good to be able to have methods with implementation in interfaces, as you can still use those as functional interfaces in streams / lambda expressions and utilize their default methods in processing. You cannot do that with classes but diamond problem still exist there. That is my guess :)
answered 2 hours ago
El Krystiano
364
364
add a comment |Â
add a comment |Â
up vote
0
down vote
Java is like a real life. You can get only your family's gen(mother and father = family) as inheritance. But you can get most of people's behaviour like your friends , your teachers or others.
add a comment |Â
up vote
0
down vote
Java is like a real life. You can get only your family's gen(mother and father = family) as inheritance. But you can get most of people's behaviour like your friends , your teachers or others.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Java is like a real life. You can get only your family's gen(mother and father = family) as inheritance. But you can get most of people's behaviour like your friends , your teachers or others.
Java is like a real life. You can get only your family's gen(mother and father = family) as inheritance. But you can get most of people's behaviour like your friends , your teachers or others.
edited 2 hours ago
answered 2 hours ago
drowny
1,226314
1,226314
add a comment |Â
add a comment |Â
up vote
0
down vote
The language designers already thought about that, so these things are enforced by the compiler. So if you define:
interface First
default void go()
interface Second
default void go()
And you implement a class for both interfaces:
static class Impl implements First, Second
you will get a compilation error; and you would need to override go
to not create the ambiguity around it.
But you could be thinking that you can trick the compiler here, by doing:
interface First
public default void go()
static abstract class Second
abstract void go();
static class Impl extends Second implements First
You could think that First::go
already provides an implementation for Second::go
and it should be fine. This is too taken care of, thus this does not compile either.
JLS 9.4.1.3 : Similarly, when an abstract and a default method with matching signatures are inherited, we produce an error. In this case, it would be possible to give priority to one or the other - perhaps we would assume that the default method provides a reasonable implementation for the abstract method, too. But this is risky, since other than the coincidental name and signature, we have no reason to believe that the default method behaves consistently with the abstract method's contract - the default method may not have even existed when the subinterface was originally developed. It is safer in this situation to ask the user to actively assert that the default implementation is appropriate (via an overriding declaration).
The last point I would bring in, to solidify that multiple inheritance is not allowed even with new additions in java, is that static methods from interfaces are not inherited. static methods are inherited by default:
static class Bug
static void printIt()
System.out.println("Bug...");
static class Spectre extends Bug
static void test()
printIt(); // this will work just fine
But if we change that for an interface (and you can implement multiple interfaces, unlike classes):
interface Bug
static void printIt()
System.out.println("Bug...");
static class Spectre implements Bug
static void test()
printIt(); // this will not compile
Now, this is prohibited by the compiler and JLS
too:
JLS 8.4.8 : A class does not inherit static methods from its superinterfaces.
add a comment |Â
up vote
0
down vote
The language designers already thought about that, so these things are enforced by the compiler. So if you define:
interface First
default void go()
interface Second
default void go()
And you implement a class for both interfaces:
static class Impl implements First, Second
you will get a compilation error; and you would need to override go
to not create the ambiguity around it.
But you could be thinking that you can trick the compiler here, by doing:
interface First
public default void go()
static abstract class Second
abstract void go();
static class Impl extends Second implements First
You could think that First::go
already provides an implementation for Second::go
and it should be fine. This is too taken care of, thus this does not compile either.
JLS 9.4.1.3 : Similarly, when an abstract and a default method with matching signatures are inherited, we produce an error. In this case, it would be possible to give priority to one or the other - perhaps we would assume that the default method provides a reasonable implementation for the abstract method, too. But this is risky, since other than the coincidental name and signature, we have no reason to believe that the default method behaves consistently with the abstract method's contract - the default method may not have even existed when the subinterface was originally developed. It is safer in this situation to ask the user to actively assert that the default implementation is appropriate (via an overriding declaration).
The last point I would bring in, to solidify that multiple inheritance is not allowed even with new additions in java, is that static methods from interfaces are not inherited. static methods are inherited by default:
static class Bug
static void printIt()
System.out.println("Bug...");
static class Spectre extends Bug
static void test()
printIt(); // this will work just fine
But if we change that for an interface (and you can implement multiple interfaces, unlike classes):
interface Bug
static void printIt()
System.out.println("Bug...");
static class Spectre implements Bug
static void test()
printIt(); // this will not compile
Now, this is prohibited by the compiler and JLS
too:
JLS 8.4.8 : A class does not inherit static methods from its superinterfaces.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The language designers already thought about that, so these things are enforced by the compiler. So if you define:
interface First
default void go()
interface Second
default void go()
And you implement a class for both interfaces:
static class Impl implements First, Second
you will get a compilation error; and you would need to override go
to not create the ambiguity around it.
But you could be thinking that you can trick the compiler here, by doing:
interface First
public default void go()
static abstract class Second
abstract void go();
static class Impl extends Second implements First
You could think that First::go
already provides an implementation for Second::go
and it should be fine. This is too taken care of, thus this does not compile either.
JLS 9.4.1.3 : Similarly, when an abstract and a default method with matching signatures are inherited, we produce an error. In this case, it would be possible to give priority to one or the other - perhaps we would assume that the default method provides a reasonable implementation for the abstract method, too. But this is risky, since other than the coincidental name and signature, we have no reason to believe that the default method behaves consistently with the abstract method's contract - the default method may not have even existed when the subinterface was originally developed. It is safer in this situation to ask the user to actively assert that the default implementation is appropriate (via an overriding declaration).
The last point I would bring in, to solidify that multiple inheritance is not allowed even with new additions in java, is that static methods from interfaces are not inherited. static methods are inherited by default:
static class Bug
static void printIt()
System.out.println("Bug...");
static class Spectre extends Bug
static void test()
printIt(); // this will work just fine
But if we change that for an interface (and you can implement multiple interfaces, unlike classes):
interface Bug
static void printIt()
System.out.println("Bug...");
static class Spectre implements Bug
static void test()
printIt(); // this will not compile
Now, this is prohibited by the compiler and JLS
too:
JLS 8.4.8 : A class does not inherit static methods from its superinterfaces.
The language designers already thought about that, so these things are enforced by the compiler. So if you define:
interface First
default void go()
interface Second
default void go()
And you implement a class for both interfaces:
static class Impl implements First, Second
you will get a compilation error; and you would need to override go
to not create the ambiguity around it.
But you could be thinking that you can trick the compiler here, by doing:
interface First
public default void go()
static abstract class Second
abstract void go();
static class Impl extends Second implements First
You could think that First::go
already provides an implementation for Second::go
and it should be fine. This is too taken care of, thus this does not compile either.
JLS 9.4.1.3 : Similarly, when an abstract and a default method with matching signatures are inherited, we produce an error. In this case, it would be possible to give priority to one or the other - perhaps we would assume that the default method provides a reasonable implementation for the abstract method, too. But this is risky, since other than the coincidental name and signature, we have no reason to believe that the default method behaves consistently with the abstract method's contract - the default method may not have even existed when the subinterface was originally developed. It is safer in this situation to ask the user to actively assert that the default implementation is appropriate (via an overriding declaration).
The last point I would bring in, to solidify that multiple inheritance is not allowed even with new additions in java, is that static methods from interfaces are not inherited. static methods are inherited by default:
static class Bug
static void printIt()
System.out.println("Bug...");
static class Spectre extends Bug
static void test()
printIt(); // this will work just fine
But if we change that for an interface (and you can implement multiple interfaces, unlike classes):
interface Bug
static void printIt()
System.out.println("Bug...");
static class Spectre implements Bug
static void test()
printIt(); // this will not compile
Now, this is prohibited by the compiler and JLS
too:
JLS 8.4.8 : A class does not inherit static methods from its superinterfaces.
answered 14 mins ago


Eugene
61.7k986143
61.7k986143
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52620936%2fwhy-does-java-not-allow-multiple-inheritance-but-does-allow-conforming-to-multip%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
maybe you should ask those who implemented it?
– Stultuske
2 hours ago
:) i am asking it is not allowed to implement to 2 inherit 2 abstract classes. but allowed implement interface with default methods. If it allowed to this, it should also allowed to multiple inheritance.
– Asanka
2 hours ago
2
yes, but you are asking it to a community that can only guess for the motives of those who implemented it. Why is it allowed? because someone at Oracle decided so. Though I haven't used default methods (yet), I understood there 's somewhat an order in which method to use if there are several ones, but I'm not sure which one gets favoured.
– Stultuske
2 hours ago
You should make your question clearer. You're asking twice (in the title and the body) "why java not allowed to multiple inheritance but allowed to implemented to interfaces which are already implemented?" - and the duplicate that you mention yourself is a duplicate of that. Somehow you manage to mention default methods but it's unclear what your question is. Also, your comments are not part of your question - if you feel your comment clarifies your question, edit it into your question please.
– Erwin Bolwidt
2 hours ago
ok, i'll edit it. :)
– Asanka
2 hours ago