Reputation: 1279
Well this is allowed in java
ClassA extends ClassB implements InterfaceA
and also
InterfaceA extends IntefaceB , InterfaceC
Well if these arenot multiple inheritance then what are they?
UPDATE
Well I happened to phrase the question in a wrong way. My original question was why doesn't java support multiple inheritance.
Well what I really want to know is:
if more than one interface can be extended by an interface, or a class can extend one class and implement an interface, then why can't those be called multiple inheritance?
Upvotes: 0
Views: 539
Reputation: 172458
The difference between an interface and a regular class is that you cannot specify implementation in an interface. To be more clear, you can only specify methods, but not implement them. If you want to have multiple inheritance then you have to implement
multiple interface. Java does not support multiple inheritance due to the below reasons.
Also to note that Interfaces are about subtyping and polymorphism, whereas, inheriting methods is about code reuse.
From here:
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.
You can find this interesting article on the same:-
Upvotes: 2
Reputation: 28548
As Inheritance defined as:
inheriting attributes and behavior from pre-existing classes called base classes, superclasses, or parent classes
Inheritance is to take some implementation from parent class. If a class inherit a class there must be some methods and attributes that child class inherited from base class.
But implementing an interface is not in general add some attributes or behavior.So when a class implements multiple interfaces there is no additional implementation comes from interfaces Or in other words we can say a class cannot inherit implementation from two different sources.
Reason why Multiple Inheritance is not allowed.
(reference wikipedia)
The "diamond problem" (sometimes referred to as the "deadly diamond of death") is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and/or C has overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
For example, in the context of GUI software development, a class Button may inherit from both classes Rectangle (for appearance) and Clickable (for functionality/input handling), and classes Rectangle and Clickable both inherit from the Object class. Now if the equals method is called for a Button object and there is no such method in the Button class but there is an overridden equals method in Rectangle or Clickable (or both), which method should be eventually called?
It is called the "diamond problem" because of the shape of the class inheritance diagram in this situation. In this article, class A is at the top, both B and C separately beneath it, and D joins the two together at the bottom to form a diamond shape.
Upvotes: 2
Reputation: 200168
Java does support multiple inheritance; just take note that the support is very constrained: you can only inherit more than one interface. That is why you have heard that Java is single-inheritance: it is single class inheritance.
NB Java 8 will push its design even closer to multiple inheritance: interface
will be allowed to define method implementations and a class will indeed inherit implementation from multiple parents. The diamond problem will be efficiencly solved by requiring the class having the conflict override the method. Within that override, the child class will be able to refer to each supertype implementation individually.
Thus, as of version 8, Java can be said to almost posses full multiple inheritance of implementation, just with manual resolution of conflicts, unlike C++ and other languages, which specify a formula for how the compiler will automatically resolve them.
Upvotes: 6
Reputation: 236014
Java has no support for multiple inheritance. To explain the first example:
ClassA extends ClassB implements InterfaceA
In here, ClassA
is extending a single class, and implementing a single interface. Whereas in the second case:
InterfaceA extends IntefaceB, InterfaceC
An interface is declared to extend from two other interfaces, you can think of this as "adding" all the methods from the extended interfaces, but it's not really multiple inheritance, the class that implements InterfaceA
will have to provide implementations for all the methods defined in InterfaceA
, InterfaceB
and InterfaceC
.
Upvotes: 1