Why multiple inheritance is not recommended in java
class BaseCol
{
public:
virtual void a() { cout << “BaseCol::a” << endl; }
virtual void b() { cout << “BaseCol::b” << endl; }
};
class ChildCol1 : public BaseCol
{
public:
virtual void a() { cout << “ChildCol1::a” << endl; }
virtual void b() { cout << “ChildCol1::b” << endl; }
};
class ChildCol2 : public BaseCol
{
public:
virtual void a() { cout << “ChildCol2::a” << endl; }
virtual void b() { cout << “ChildCol2::b” << endl; }
};
class Row : public ChildCol1, public ChildCol2
{
public:
// i want to overide the b function of ChildCol1 only !!
void b() { cout << “Row1::b” << endl; }
};
While executing the above code at runtime ambiguity(Doubtfulness or uncertainty as regards interpretation) error will occur since the implementation has conflicting methods. This problem is called “Diamond inheritance problem“.
To avoid this complexity Java provides single inheritance with interface concept. Since interface cannot have method implementation, the problem is therefore avoided since there is always only one implementation to a specific method and hence no ambiguity will arise
I have to thank you for the efforts you have put in penning this blog.
I’m hoping to check out the same high-grade content
by you later on as well. In truth, your creative writing abilities has inspired me to get my own website now 😉
Thanks. Please like it and share to your friends.
https://www.facebook.com/sharepoint.wordpress