Reputation: 632
I have three classes
ChildTwo
public class ChildTwo {
public void methodOfChildTwo() {
System.out.println("methodOfChildTwo.");
}
}
ChildOne which extends ChildTwo
public class ChildOne extends ChildTwo{
public void methodOfChildOne() {
System.out.println("methodOfChildOne.");
}
}
Parent which extends ChildOne
public class Parent extends ChildOne{
public void methodOfParent() {
System.out.println("methodOfParent.");
}
}
When I create object of Parent, I can call all the three methods as all those methods are inherited in Parent class.
Parent parent =new Parent();
parent.methodOfParent();
parent.methodOfChildOne();
parent.methodOfChildTwo();
My questions are
Parent extends ChildOne,ChildTwo
(Java does not supports these syntax). Why Java does not supports this syntax, although we can achieve this in above mentioned way?Please someone clarify.
Upvotes: 1
Views: 284
Reputation: 17454
First of all Java does not support multiple inheritance because it may cause ambiguity. (Note: language like C++ does support multiple inheritance).
1.Can I say this way I can achieve multiple inheritance in Java?
This is still not multiple inheritance.
See my diagram below:
Your given code is like the diagram on the right hand side. Multiple inheritance will inherit all properties and behaviours directly from all its parents (Not supported in Java).
2.This approach is conceptually similar with multiple extends, like Parent extends ChildOne,ChildTwo (Java does not supports these syntax).
Your approach is actually not conceptually similar with multiple extends. Java does not support this to prevent ambiguity. Imagine both parent class has a method of similar signature.
class Bomb{
public void activate();
}
class LightBulb{
public void activate();
}
class Child extends Bomb, LightBulb{ //Imagine if this is allowed
//When I call activate(), will it activate the bomb or the lightBulb?
}
Why Java does not supports this syntax, although we can achieve this in above mentioned way?
Both cases are different, you can't achieve multiple inheritance by a extends b
, b extends c
. Conceptually it is different because the hierarchy is totally different.
In multiple inheritance, both parent class which you want to extends to can be totally unrelated. Imagine Pegasus extends FlyingCreature, Horse
.
Pegasus
is a FlyingCreature
, it is also a Horse
, but FlyingCreature
and Horse
are not related at all.
In your given example, all subsequent exntended parent classes is a subset of another. They are all related.
Mammal
is Animal
and Lion
is Mammal
and is also Animal.
If you say your mentioned approach is conceptually similar to multiple inheritance, think of this scenario:
You are tasked to create class Pegasus
from class FlyingCreature
& class Horse
.
Are you going to do this?
class FlyingCreature{
}
class Horse extends FlyingCreature{ //But Horses do not fly!
}
class Pegasus extends Horse{ //You got what you want here, but the Horse class is wrong.
}
Or this?
class Horse{
}
class FlyingCreature extends Horse{ //All flying creatures are horses? Are you sure?
}
//You got what you want here, but the FlyingCreature class is wrong.
class Pegasus extends FlyingCreature {
}
So now you see, it can't be done because both parent class are not related at all. To somewhat achieve so called "multiple inheritance", Java use interface
.
Upvotes: 2
Reputation: 477
Your example is multilevel inheritence, not multiple inheritance.
Think of inheritance like having parents. You inherit what your parents have, and in turn inherit what they got from their grandparents. You wouldn't call that multiple inheritance, would you?
Multiple class inheritance would be like getting genes from your parents and then other parents (from a different family tree). Doesn't make sense in real life because what if you got duplicate genes? If you got blue eye genes from one set of parents and green eye genes from your other parents, which would you pick?
It's the same thing in Java:
Here's a simple example:
public class A {
public void doSomething() {
// Does something
}
}
public class B {
public void doSomething() {
// Does something else
}
}
public class C extends A,B {
....
}
public static void main(String[] args) {
C newC = new C();
newC.doSomething();
// PROBLEM!
}
Which doSomething()
should be picked? There's no way to tell! So Java avoids this problem (and many others I'm sure) by not allowing multiple superclasses.
Upvotes: 0
Reputation: 2953
The one you explained is "Multilevel Inheritance".
Multilevel Inheritance -> B extends A. C extends B. Multiple Inheritance -> P extends Q, R. This is not allowed in java. For multiple inheritance we use Interfaces. As a class can implement any number of interfaces.
Upvotes: 0
Reputation: 3620
No it is not conceptually similar.
While Parent
has extended ChildOne
which in turn extends ChildTwo
and inherited methods from both, it causes following problem:
ChildOne[] arr = new ChildOne[2];
arr[0] = new ChildOne();
arr[1] = new ChildTwo(); // <--- this should have failed
Assuming that ChildOne
and ChildTwo
are supposed to be mutually exclusive, the last assignment arr[1] = c2
should have failed with an ArrayStoreException.
Upvotes: 0
Reputation: 200168
A Dog
is both a Mammal
and a Pet
. Yet Pet
is not Mammal
. You cannot model the relationship between Mammal
, Pet
, and Dog
using only Java classes. This is because Java does not support multiple inheritance.
The above is not conceptually similar to the relationship between Animal
, Mammal
, and Dog
, which is something Java supports. This is why your "although" part of the question is a non-sequitur.
Upvotes: 0
Reputation: 5168
See this thread about the same problem: Why is Multiple Inheritance not allowed in Java or C#?
The best answer is too much trouble for so little benefits. You can pratically made the same things with the interfaces.
You can implements multiple interfaces, so there is no need to have multiple inheritance.
Upvotes: 0