Java - why java not allowed to multiple inheritance but allowed to implemented to interfaces which are already implemented

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
2












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.










share|improve this question























  • 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














up vote
6
down vote

favorite
2












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.










share|improve this question























  • 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












up vote
6
down vote

favorite
2









up vote
6
down vote

favorite
2






2





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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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 a A and B class that you want to subclass doesn't have the same meaning and intention.





share|improve this answer





























    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.






    share|improve this answer



























      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 :)






      share|improve this answer



























        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.






        share|improve this answer



























          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.






          share|improve this answer






















            Your Answer





            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            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






























            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 a A and B class that you want to subclass doesn't have the same meaning and intention.





            share|improve this answer


























              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 a A and B class that you want to subclass doesn't have the same meaning and intention.





              share|improve this answer
























                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 a A and B class that you want to subclass doesn't have the same meaning and intention.





                share|improve this answer














                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 a A and B class that you want to subclass doesn't have the same meaning and intention.






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 1 hour ago









                user7

                8,09032038




                8,09032038










                answered 1 hour ago









                davidxxx

                57.7k54379




                57.7k54379






















                    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.






                    share|improve this answer
























                      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.






                      share|improve this answer






















                        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.






                        share|improve this answer












                        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.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 1 hour ago









                        Peter Lawrey

                        430k55539921




                        430k55539921




















                            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 :)






                            share|improve this answer
























                              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 :)






                              share|improve this answer






















                                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 :)






                                share|improve this answer












                                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 :)







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered 1 hour ago









                                El Krystiano

                                364




                                364




















                                    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.






                                    share|improve this answer
























                                      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.






                                      share|improve this answer






















                                        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.






                                        share|improve this answer












                                        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.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered 1 hour ago









                                        S.K.

                                        1,905717




                                        1,905717




















                                            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.






                                            share|improve this answer


























                                              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.






                                              share|improve this answer
























                                                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.






                                                share|improve this answer














                                                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.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited 1 hour ago

























                                                answered 1 hour ago









                                                drowny

                                                1,226314




                                                1,226314



























                                                     

                                                    draft saved


                                                    draft discarded















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    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













































































                                                    Comments

                                                    Popular posts from this blog

                                                    What does second last employer means? [closed]

                                                    List of Gilmore Girls characters

                                                    Confectionery