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();
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”.
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++.
However this problem is resolved in C++ through pointers and virtual methods, So multiple inheritance allowed in C++.
Hope it helps!
Happy Coding J
Happy Coding J
Interesting, so it's compiler problem.
ReplyDeleteThanks for posting.
Nice.
ReplyDeleteThanks for post.
No! It's not compiler problem .Compiler designers not implement multiple inheritance in C# or Java because due to it complexities increases.
ReplyDeleteThanks Muneeb :)
ReplyDeleteNice Boy!Keep it Up :)
ReplyDeleteNICE ONE SHOAIB:)KEEP IT UP BOY!!
ReplyDeleteWow..that's easy to understand ! Thanks !
ReplyDeleteNice One !!
ReplyDeletehmm..Good yar...Keep it up...
ReplyDeletegr8 work buddy!
ReplyDeleteIf I am correct the problem is resolved via virtual inheritance not via virtual methods?
ReplyDeletebut 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.
Yes It,s Virtual Inheritance.
ReplyDeleteIf 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!
Good One!
ReplyDeleteThanks....
ReplyDeleteyes 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.
ReplyDeletemoreover 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.
Good One
ReplyDeletegr8 work by all of u and thanx 4 posting it...................
ReplyDeletenice answer..........thank u..
ReplyDeleteThanks Ganesh...
ReplyDeleteusing System;
ReplyDeleteclass 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
Sorry brother I read your question now so sorry for late reply.
ReplyDeleteNow 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.
thank you so much... that's best n simplest explanation so far i got about multiple inheritance... :)
ReplyDeleteMake use of virtual methods. Dimonnd inheritance is then resolved
ReplyDeleteArvind kumar vais
ReplyDeleteVery good Answer sir i am satisfied
Great blog Shoaib, you help me to understand well
ReplyDeleteGood base class constructor call first then derived class.
ReplyDeleteGreat work yar...simple explanation and easy to understand...
ReplyDelete