Java does not support multiple inheritance to avoid complexity and ambiguity problems. Interfaces were introduced instead, allowing a class to represent different structures by implementing multiple interfaces. In C++, multiple inheritance can cause ambiguity errors at runtime if two parent classes define the same method. Java's single inheritance with interfaces avoids this problem by not allowing method implementations in interfaces, so there is only one implementation per method.
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
How would you implement multiple inheritance in java
1. How would you implement multiple
inheritance in Java?
Multiple inheritance is the ability to inherit from multiple classes. Java does not have this
capability.
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 { /* ... */ };
Why doesn't the Java language support multiple inheritance?
Whenever you find yourself asking why Java has or does not have some feature, consider
the design goals behind the Java language. With that in mind, I started my search by
skimming through "The Java Language Environment" by James Gosling and Henry McGilton
(Sun Microsystems), a white paper published in May 1996 that explains some of the
reasoning behind Java's design.
As the white paper states, the Java design team strove to make Java:
• Simple, object oriented, and familiar
• Robust and secure
• Architecture neutral and portable
• High performance
• Interpreted, threaded, and dynamic
The reasons for omitting multiple inheritance from the Java language mostly stem from the
"simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a
language that most developers could grasp without extensive training. To that end, they
worked to make the language as similar to C++ as possible (familiar) without carrying over
C++'s unnecessary complexity (simple).
In the designers' opinion, multiple inheritance causes more problems and confusion than it
solves. So they cut multiple inheritance from the language (just as they cut operator
overloading). The designers' extensive C++ experience taught them that multiple inheritance
just wasn't worth the headache.
2. Note: For a discussion of the diamond problem, a classic multiple inheritance challenge,
read Bill Venners's "Designing with Interfaces" (JavaWorld, December 1998) and Tony
Sintes's "Java Diamonds Are Forever" (JavaWorld, March 2001).
Instead, Java's designers chose to allow multiple interface inheritance through the use of
interfaces, an idea borrowed from Objective C's protocols. Multiple interface inheritance
allows an object to inherit many different method signatures with the caveat that the
inheriting object must implement those inherited methods.
Multiple interface inheritance still allows an object to inherit methods and to behave
polymorphically on those methods. The inheriting object just doesn't get an implementation
free ride. For an excellent discussion of interface inheritance, read Wm. Paul Rogers's
"Reveal the Magic Behind Subtype Polymorphism
When Sun was designing Java, it omitted multiple inheritance - or more precisely multiple
implementation inheritance - on purpose. Yet multiple inheritance can be useful, particularly
when the potential ancestors of a class have orthogonal concerns. This article presents a utility
class that not only allows multiple inheritance to be simulated, but also has other far-reaching
applications.
Have you ever found yourself wanting to write something similar to:
public class Employee extends Person, Employment {
// detail omitted
}
Here, Person is a concrete class that represents a person, while Employment is another concrete
class that represents the details of a person who is employed. If you could only put them together,
you would have everything necessary to define and implement an Employee class. Except in Java
- you can't. Inheriting implementation from more than one superclass - multiple implementation
inheritance - is not a feature of the language. Java allows a class to have a single superclass and no
more.
On the other hand, a class can implement multiple interfaces. In other words, Java supports
multiple interface inheritance. Suppose the PersonLike interface is:
public interface PersonLike {
String getName();
int getAge();
}
and the EmployeeLike interface is:
public interface EmployeeLike {
float getSalary();
java.util.Date getHireDate();
}
3. Introduction
Java was designed without multiple inheritance. While some developers think of this as a
flaw, it is actually true that the overall design of Java supports the solution of problems
commonly solved with multiple inheritance in other ways. In particular, the singly rooted
hierarchy (with Object as the ultimate ancestor of all classes) and Java interfaces solves
most problems that are commonly solved using multiple inheritance in C++.
However, there are a few situations in which multiple inheritance is very helpful. In this
note we will consider one special case and also the general case of multiple inheritance.
Mixin Inheritance
In mixin inheritance, one class is specifically designed to be used as one of the classes in
a multiple inheritance scheme. We say that it provides some functionality that is "mixed
in" to some other class that wants this functionality. Another way to think of mixin
inheritance is that a mixin class is given a new parent class so that the mixin seems to
extend the other class. In some projects it is necessary to rely on common services that
must be provided by several classes. Mixin inheritance is one way to centralize the
development of these services.
To provide for mixin inheritance we will need to define two interfaces as well a at least
one class that provides the service: the Mixin class. In some situations, one of these
interfaces is empty and may then be omitted.
The Interfaces
The first interface (always required) defines what the mixin class will provide for
services. It defines one or more methods that will be implemented by the mixin class. We
will take a simple and abstract example here. The class will be called (abstractly)
MProvides to emphasize that it defines what any compatible mixin must provide. We will
also assume that the only service provided is a void function and we will give it the
abstract name func. In practice, however, there may be any number of methods defined
and they may have any signatures.
interface MProvides
{ void func();
}
One special feature of mixin inheritance that is not usually present in the general multiple
inheritance case is that the mixin class may require services of the class into which it is
mixed. That is, in order to provide the "func" service, the mixin may need to get some
information from the other class. We define this with another interface. We will give it
the abstract name MRequires to indicate that it requires one or more services from the
class into which it is mixed.
4. Here we will suppose that the compatible mixins require that the other class provides a
method getValue that returns an int.
interface MRequires
{ int getValue();
}
Why multiple inheritance is not allowed
in Java?
For long time I had a question “why Sun introduced Interface concept instead of C++ style of multiple
inheritance?“. I did googling but many articles and forums talks about difference between abstract class
and Interface not why Interface concept required in Java. After extensive search and analysis I came to
know the reason behind the Interface concept in Java.
In C++ multiple inheritance is possible, virtual keyword is been used to define virtual function. Whereas in
Java multiple inheritance is not possible however using Interface concept the same class can represent
different structure.
Lets take an example in C++ where two parent class contains same virtual function.
Example:
1 class BaseCol
2 {
3 public:
4 virtual void a() { cout << "BaseCol::a" << endl; }
5 virtual void b() { cout << "BaseCol::b" < endl; }
6 };
7
8 class ChildCol1 : public BaseCol
9 {
10 public:
11 virtual void a() { cout << "ChildCol1::a" << endl; }
12 virtual void b() { cout << "ChildCol1::b" << endl; }
13 };
14
15 class ChildCol2 : public BaseCol
16 {
17 public:
18 virtual void a() { cout << "ChildCol2::a" << endl; }
19 virtual void b() { cout << "ChildCol2::b" << endl; }
20 };
21
22 class Row : public ChildCol1, public ChildCol2
5. 23 {
24 public:
25 // i want to overide the b function of ChildCol1 only !!
26 void b() { cout << "Row1::b" << endl; }
27 };
While executing the above code at runtime ambiguity 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.