Friday, 9 September 2011

Why Multiple Inheritance Not Allowed in C# or Java In Classes?

I took this topic because this question is asked to me in my recent interview and I have no answer
Now first we understand what is multiple inheritance

What is Multiple Inheritance?

Multiple inheritance is one of the feature of object oriented concepts or programming.In Multiple inheritance we inherit class with more than one classes that is not allowed but allow in interfaces,what is Interfaces this is another topic may be discussed later.

Example:

Class A
{}

Class B
{}

Class C : A,B  // This is not allowed in C# or Java called multiple inheritance
{}

In above code we declare two classes A,B and declare another class C which is inherited by A and B Both So it is not allowed.

Why?

We consider one example

Class A
{
Public Void MethodA(){……}
}

Class B:A
{ 
}

Class C:A
{ 
}

Class BC: B, C
{ 
}

What is the problem in this example?

The problem is that when we make object of “BC” class there is duplication of Sub object of “A“ class.

BC obj=new BC();

And when we call the method.

obj.MethodA();

This will result in compiler error, because the compiler does not know whether the call to MethodA refers to the copy of “B” class or refers to the copy of “C” class. So, the call to MethodA in the code above is ambiguous and will not get past the compiler. This scenario is also called “Diamond Inheritance Problem”.

Take a look diagrammatically:



That’s why multiple inheritance is not allowed,If compiler designer try to solve this issue the complexities increase too much.

However this problem is resolved in C++ through pointers and virtual methods, So multiple inheritance allowed in C++.

Hope it helps!

Happy Coding 
J

27 comments:

  1. Interesting, so it's compiler problem.
    Thanks for posting.

    ReplyDelete
  2. No! It's not compiler problem .Compiler designers not implement multiple inheritance in C# or Java because due to it complexities increases.

    ReplyDelete
  3. Nice Boy!Keep it Up :)

    ReplyDelete
  4. NICE ONE SHOAIB:)KEEP IT UP BOY!!

    ReplyDelete
  5. Wow..that's easy to understand ! Thanks !

    ReplyDelete
  6. hmm..Good yar...Keep it up...

    ReplyDelete
  7. If I am correct the problem is resolved via virtual inheritance not via virtual methods?

    but TWO more question? What about if B is inheriting as a public and C is inheriting as a private? how the pointers resolve this problem? Could you give an example?
    -Sohail.

    ReplyDelete
  8. Yes It,s Virtual Inheritance.

    If B is Inheriting as a Public than no problem and if C is Inheriting as Private there is no problem also but we can't access public method of super base class A.
    Because inheriting class as private, the whole members of its base class either public becomes private.


    However to access private members of class we use type casting to the pointer object of class to the pointer of specific datatype of member.

    Please Make Me correct if i am wrong!

    ReplyDelete
  9. yes multiple inheritance not alloweded in C# nor java b/c A and B can have same name methods or fields when sibling class inherits both base classes then the problem arise which base class field or method to call. like if A class field named "name" and B class also have field named "name" then which field to refer.

    moreover in inheritance we mean "is a " relationship like C is A and B is A also or can be. it can be explain easyly in structural relationship.

    ReplyDelete
  10. gr8 work by all of u and thanx 4 posting it...................

    ReplyDelete
  11. nice answer..........thank u..

    ReplyDelete
  12. using System;
    class abc
    {
    int a1;
    public abc (int a)
    {
    a1 = a;
    }
    }

    class xyz:abc
    {
    int x1;
    public xyz (int x)
    {
    x1 = x;
    }
    }
    class demo
    {
    public static void Main ()
    {
    xyz ob1 = new xyz(10);


    }

    }

    Sir i want to know that what is error in my coding...
    Sir please answer as soon as possible

    ReplyDelete
  13. Sorry brother I read your question now so sorry for late reply.


    Now come to your question.
    In your code there is some mistake.

    You make object of XYZ class and ABC Class is base class of XYZ class.

    In your scenario your code could not compile because you have no empty parameter constructor of ABC class which is required to call
    if you are not calling ABC constructor explicitly from the XYZ class so you could do two things to overcome this problem

    1)Make empty constructor in ABC Class
    Like this
    class abc
    {
    int a1;
    public abc(int a)
    {
    a1 = a;
    }
    public abc()
    {
    }
    }

    class xyz:abc
    {
    int x1;
    public xyz(int x)
    {
    x1 = x;
    }
    }
    class demo
    {
    public static void Main()
    {
    xyz ob1 = new xyz(10);

    }

    }
    2.Call ABC constructor explicitly from XYZ class
    like this

    class abc
    {
    int a1;
    public abc(int a)
    {
    a1 = a;
    }
    }

    class xyz:abc
    {
    int x1;
    public xyz(int x):base(x)
    {
    x1 = x;
    }
    }
    class demo
    {
    public static void Main()
    {
    xyz ob1 = new xyz(10);


    }
    }

    Please feel free to ask if found some mistake or ambiguities.

    ReplyDelete
  14. thank you so much... that's best n simplest explanation so far i got about multiple inheritance... :)

    ReplyDelete
  15. Make use of virtual methods. Dimonnd inheritance is then resolved

    ReplyDelete
  16. Arvind kumar vais

    Very good Answer sir i am satisfied

    ReplyDelete
  17. Great blog Shoaib, you help me to understand well

    ReplyDelete
  18. Good base class constructor call first then derived class.

    ReplyDelete
  19. Great work yar...simple explanation and easy to understand...

    ReplyDelete