Multiple inheritance is the ability of a single class to inherit from multiple classes. Java doesnot have this capability.
The designers of Java considered multiple inheritance to be too complex, and not in line with the goal of keeping Java simple.
One specific problem that Java avoids by not having multiple inheritance is called the diamond problem. You can read more about that problem here:Java diamond problem.
What a Java class does have is the ability to implement multiple interfaces – which is considered a reasonable substitute for multiple inheritance, but without the added complexity. This is what a Java class that implements multiple interfaces would look like:
// Wolf and Canine are interfacespublic class Dog implements Wolf, Canine {/*...*/}
You can also have a class that extends one class, while implementing an interface – or even multiple interfaces. So, a class like this is perfectly legal in Java:
// Wolf is an interface// Dog is a base class// Poodle both extends from a class and implements// an interfacepublic class Poodle extends Dog implements Wolf {/*...*/}
So, remember that although Java does not have multiple inheritance, a Java classdoes have the ability to implement multiple interfaces instead.
However, C++ does support multiple inheritance. Syntactically, multiple inheritance in C++ would look like this, where class X derives from 3 classes – A, B, and C:
class X : public A, private B, public C { /* ... */ };
Would you like to thankProgrammerInterview.com for being a helpful free resource?Then why not tell a friend about us, orsimply add a link to this page from your webpage using the HTML below.
Link to this page:
Please bookmark with social media, your votes are noticed and appreciated: