Java - why java not allowed to multiple inheritance but allowed to implemented to interfaces which are already implemented
Clash Royale CLAN TAG#URR8PPP
up vote
6
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
6
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
1 hour 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
1 hour ago
1
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
1 hour 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
1 hour ago
ok, i'll edit it. :)
– Asanka
1 hour ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
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 24 mins ago


Talendar
613215
613215
asked 1 hour ago
Asanka
309110
309110
maybe you should ask those who implemented it?
– Stultuske
1 hour 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
1 hour ago
1
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
1 hour 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
1 hour ago
ok, i'll edit it. :)
– Asanka
1 hour ago
add a comment |Â
maybe you should ask those who implemented it?
– Stultuske
1 hour 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
1 hour ago
1
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
1 hour 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
1 hour ago
ok, i'll edit it. :)
– Asanka
1 hour ago
maybe you should ask those who implemented it?
– Stultuske
1 hour ago
maybe you should ask those who implemented it?
– Stultuske
1 hour 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
1 hour 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
1 hour ago
1
1
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
1 hour 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
1 hour 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
1 hour 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
1 hour ago
ok, i'll edit it. :)
– Asanka
1 hour ago
ok, i'll edit it. :)
– Asanka
1 hour ago
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
6
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.
add a comment |Â
up vote
3
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
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
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
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 |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
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.
add a comment |Â
up vote
6
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.
add a comment |Â
up vote
6
down vote
up vote
6
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 1 hour ago
user7
8,09032038
8,09032038
answered 1 hour ago


davidxxx
57.7k54379
57.7k54379
add a comment |Â
add a comment |Â
up vote
3
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
3
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
3
down vote
up vote
3
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 1 hour ago
Peter Lawrey
430k55539921
430k55539921
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 1 hour ago
El Krystiano
364
364
add a comment |Â
add a comment |Â
up vote
1
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
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
up vote
1
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 1 hour ago
S.K.
1,905717
1,905717
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 1 hour ago
answered 1 hour ago
drowny
1,226314
1,226314
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%2fjava-why-java-not-allowed-to-multiple-inheritance-but-allowed-to-implemented-t%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
1 hour 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
1 hour ago
1
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
1 hour 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
1 hour ago
ok, i'll edit it. :)
– Asanka
1 hour ago