Home > Uncategorized > Why multiple inheritance is not recommended in java

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

Advertisements
Categories: Uncategorized
  1. July 27, 2014 at 5:15 am

    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 😉

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: