Java-Multiple inheritance

Hello CEans. Can anybody please help me know what is the problem with multiple inheritance. Why Java does't support it?

Replies

  • Kaustubh Katdare
    Kaustubh Katdare
    Well, most probably because the designers of Java wanted it to keep it a simple, object oriented language. ๐Ÿ˜€

    Java does support multiple inheritance through 'interfaces':

    Check: #-Link-Snipped-#

    and

    #-Link-Snipped-#
  • shalini_goel14
    shalini_goel14
    Check the following scenario:
    class A{
    void method1(){
    //some code here
    }
    }

    class B{
    void method1(){
    // some code here
    }
    class C extends A,B { //do not try..not possible in java
    //assuming it inherits method1() method

    }
    class JavaMultipleInheritenceTest{
    public static void main(String args[]){
    C cObj=new C();
    cObj.method1();//creates an ambiguous situation for JVM to detect which
    class's overridden method method1() it is asking for(classA or class B)

    }
    }
    This is the reason in Java we cannot extend multiple classes but can extend multiple interfaces.
  • viktor_ns
    viktor_ns
    shalini_goel14
    Check the following scenario:
    class A{
    void method1(){
    //some code here
    }
    }

    class B{
    void method1(){
    // some code here
    }
    class C extends A,B { //do not try..not possible in java
    //assuming it inherits method1() method

    }
    class JavaMultipleInheritenceTest{
    public static void main(String args[]){
    C cObj=new C();
    cObj.method1();//creates an ambiguous situation for JVM to detect which
    class's overridden method method1() it is asking for(classA or class B)

    }
    }
    This is the reason in Java we cannot extend multiple classes but can extend multiple interfaces.

    The problem you defined is simple to solve. It just uses simple method renaming so they have different names. Dynamic binding would know what method to call in the case of polymorphism despite the renaming.

    The more complicated problem to solve is the case of repeated inheritance where one class directly or indirectly inherits several times from the same class. But even for this there is elegant solution as implemented in Eiffel.
  • xero
    xero
    Starting with what's the problem with multiple inheritance.

    Let's start with a funny scenario. Consider a family, comprising of a child and his/her mom and dad

    A common behavior of mom and dad is that both are short tempered, with a difference that child's dad starts breaking things when angry, and mom only yells.

    Now, the child's also short tempered, but its impossible to know whether the short tempered behavior is his/her's mom's or dad's (assuming that child won't get both the behaviors ๐Ÿ˜ )

    So I guess, the problem with multiple inheritance is pretty clear ๐Ÿ˜€


    Second question: Why java doesn't support it?

    I would rephrase the question, Why java doesn't allow it?
    In Java, the above scenario is resolved as ...
    in java, if the child is short tempered then its behavior must be known before its born !

    Now, as stated in above replies that multiple inheritance is achieved via interfaces.
    Interfaces are not objects, and inheritance only exists between objects, so i guess its no more multiple inheritance ๐Ÿ˜€

    I think this should answer your question.. By the way, i hate explaining OOP concepts with code, so if u were expecting it, m sorry, I won't ๐Ÿ˜€

    n'joy
  • prakash.athani
    prakash.athani
    Do you guys think that multiple inheritence is an ambiguous feature that an OO language should not allow?

    C++ allows multiple inheritence.

    Check out this paper by Bjarne Stroustrup (inventor of C++) where he mentions its misconception.

    #-Link-Snipped-#

    Also for the scenarios mentioned by shalini_goel14 and xero, we just need a scope resolution operator :: to decide which parent class's method should a child class's object invoke

    Also can you guys give me examples of some systems developed using C++,
    having problems or crashed, because of its multiple inheritence feature ?
  • xero
    xero
    I'll start with clearing a mis conception that inheritance is a feature. Inheritance is a characteristic of OO paradigm, so multiple inheritance is not a feature, and it is ambiguous, but not bad ๐Ÿ˜›

    and yes C++ has its own way to implement it using virtual functions if i'm not wrong, so wherever multiple inheritance comes into picture, it would have been / will be implemented using the standard ways of C++ (either using virtual functions or any new mechanism )

    And i don't think that applications/systems will ever crash if multiple inheritance is implemented, atleast i know about those written in C++.


    And even if it does crash, then its CURSED !! ๐Ÿ˜

    n'joy
  • xero
    xero
    Hey prakash thanks for the linked. Its pretty good, i would suggest everyone to go through it once !!
  • viktor_ns
    viktor_ns
    I like this little bit written by Bertrand Meyer:

    In some parts of the object-oriented community, the mere mention of multiple inheritance seems to evoke visions of mysterious, uncharted territories where complexity is the law of the land and treacherous ambiguities lie in cover behind every bush, ready to prey on the poor wandering programmer. The purpose of this month's column is to dispel any such image and show that multiple inheritance is in fact as simple to use as it is powerful.
    Harnessing multiple inheritance

    I must point out one more thing. We do inherit features from our parents but we are not our parents. That is, child is not its mother, neither is its father. Child has a mother and has a father so that can only be a client relationship not an inheritance.

    But CHILD can inherit from classes HUMAN_THAT_YELLS and HUMAN_THAT_BREAKS_THINGS that inherit from class HUMAN. If HUMAN has method engage_short_temper() and it is redefined in both classes HTY and HTB problem can be solved in two ways. First is that in CHILD we make one method from one parent deferred and take the method only from other parent. Second is that we rename methods and select one to use with dynamic binding when engage_short_temper() is called on HUMAN that is really a CHILD.

You are reading an archived discussion.

Related Posts

hey guys... i know this is a very common problem and has been posted earlier also... but nobody seems to have figured out a solution... i am making a program...
My final year project topic is: CHARPY TOUGHNESS AND MICROSTRUCTURAL CHARACTERISTICS OF MILD STEEL WELDED JOINTS QUENCHED IN MINERAL OILS.I NEED HELP IN EXECUTING IT.
hi... I am developing an application using PHP/MySQL. here, i have one page(say P1), which contains a form(just one drop down menu, say D1, with only 2 options , say...
hey guys. this may be very trivial, but i cant figure out a way... i want to retrieve the data entered by the user through a drop-down-menu using POST method....
hi frnds. this is something which may seem foolish to ask, but i have been beating my head against the wall at the frustration of not being able to get...