Multiple inheritance stands for a class inheriting member variables and/or methods from multiple parents.

To better understand what it means, a real world example can be considered:
When a man inherits property from his parents as well from his spouse parents then it is multiple inheritance. But if the person has only inherited property from his parents then it is single inheritance. His parents could have acquired this property from his parents but that has nothing to do with multiple inheritance.

Similarly in Java, a class can extend only one class at a time and hence Java avoids the concept of multiple inheritance. But yes an inheritance hierarchy could be formed by a generation of classes like Date class extends from Object class and CustomDate extending from Date class.

Sample code

The problem with multiple inheritance is the onset of diamond problem. Let us see two very simple classes:

Class A {
   public string name="a";
}
Class B {
   public string name="b";
}

In Java we cannot have a class C defined as Class C extends A,B {...}. Had Java allowed this, there would be an ambiguity about the member variable name which has got different values in classes A,B. As with member variable name, a method defined in both the classes could have different implementation. This is known as diamond problem in OOPS. Java has avoided this ambiguity by disallowing extending from more than one class.

Multiple inheritance in other languages

Other object oriented programming languages C++ and Scala do allow multiple inheritance and allow developer to specify the member to be used when referring to a common parent member. But if developer misses it then application can have issues with the code at runtime. Java has kept itself simple by all together avoiding multiple inheritance.

Interfaces and Multiple inheritance

Though constants defined in an interface are inherited by the class implementing it, they don’t have any role to play in multiple inheritance. An interface can only have abstract methods or public static final variables. Since the interfaces can’t have variables and concrete methods, there is no ambiguity when implementing multiple interfaces.

Thus if the need to have two super types without ambiguity then class needs to use interfaces and add variables and concrete methods itself. If the class needs to inherit concrete members then it can’t use multiple inheritance.

Multiple inheritance in Java admin Core Java
Multiple inheritance stands for a class inheriting member variables and/or methods from multiple parents. To better understand what it means, a real world example can be considered: When a man inherits property from his parents as well from his spouse parents then it is multiple inheritance. But if the person...
Multiple inheritance stands for a class inheriting member variables and/or methods from multiple parents. <strong>To better understand what it means, a real world example can be considered:</strong> When a man inherits property from his parents as well from his spouse parents then it is multiple inheritance. But if the person has only inherited property from his parents then it is single inheritance. His parents could have acquired this property from his parents but that has nothing to do with multiple inheritance. Similarly in Java, a class can extend only one class at a time and hence Java avoids the concept of multiple inheritance. But yes an inheritance hierarchy could be formed by a generation of classes like Date class extends from Object class and CustomDate extending from Date class. <h2>Sample code</h2> The problem with multiple inheritance is the onset of diamond problem. Let us see two very simple classes: 1 In Java we cannot have a class C defined as <code>Class C extends A,B {...}</code>. Had Java allowed this, there would be an ambiguity about the member variable name which has got different values in classes A,B. As with member variable name, a method defined in both the classes could have different implementation. This is known as diamond problem in OOPS. Java has avoided this ambiguity by disallowing extending from more than one class. <h2>Multiple inheritance in other languages</h2> Other object oriented programming languages C++ and Scala do allow multiple inheritance and allow developer to specify the member to be used when referring to a common parent member. But if developer misses it then application can have issues with the code at runtime. Java has kept itself simple by all together avoiding multiple inheritance. <h2>Interfaces and Multiple inheritance</h2> Though constants defined in an interface are inherited by the class implementing it, they don't have any role to play in multiple inheritance. An interface can only have abstract methods or public static <a href="http://www.javaexperience.com/initializing-final-variables-in-java/" title="Initializing final variables in Java">final variables</a>. Since the interfaces can't have variables and concrete methods, there is no ambiguity when implementing multiple interfaces. Thus if the need to have two super types without ambiguity then class needs to use interfaces and add variables and concrete methods itself. If the class needs to inherit concrete members then it can't use multiple inheritance.
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.