1

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

  1. Can I say this way I can achieve multiple inheritance in Java?
  2. This approach is conceptually similar with multiple extends, like 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.

Rajorshi Roy
  • 632
  • 1
  • 7
  • 15
  • 2
    Multiple inheritance would be having `Child` extend both `Father` and `Mother`. How would you achieve that? – sstan Jul 10 '15 at 19:47
  • Multiple inheritance is something like `.<:`; what you have here is `.-.-.`. Dots represent classes, `<` and `-` represent inheritance. If you wanted an extremely rough textual explanation. – Chris Sprague Jul 10 '15 at 19:50
  • It does not support it because if both ChildOne and ChildTwo had the same method, how does it determine which method is kept and which is ignored. It may seem easy but some people may see it make sense one way over the other. By keeping it simple and just allowing single inheritance, things are kept simpler – MiltoxBeyond Jul 10 '15 at 19:50
  • It's as conceptually similar as (Grandmother, Mother, Daughter) and (Mother, Father, Daughter). – Marko Topolnik Jul 10 '15 at 19:50
  • my Parent class extend two classes – Rajorshi Roy Jul 10 '15 at 19:51
  • possible duplicate of [Java Multiple Inheritance](http://stackoverflow.com/questions/21824402/java-multiple-inheritance) – JFPicard Jul 10 '15 at 19:51
  • It only extends one class. The class that it extended, extends from another class – MiltoxBeyond Jul 10 '15 at 19:51

6 Answers6

2

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:

enter image description here

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.

user3437460
  • 17,253
  • 15
  • 58
  • 106
0

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.

Community
  • 1
  • 1
JFPicard
  • 5,029
  • 3
  • 19
  • 43
0

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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

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.

yas
  • 3,520
  • 4
  • 25
  • 38
0

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.

Raman Shrivastava
  • 2,923
  • 15
  • 26
0

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.

SwiftySwift
  • 477
  • 4
  • 13