3

I am confused regarding a concept in multiple inheritance. I have three classes A, B and C.

Class A {
    // ...
}

Class B extends A {
    // ...
}

Class C extends B {
    // ...
}

I know this is a bad practice of multiple inheritance and I also read java allows multiple inheritance through interfaces. But I am not getting any error in the above code. Please can anyone explain me with a clear example without using interface. Thanks!!

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
JavaQuest
  • 671
  • 6
  • 23
  • 2
    "...bad practice of multiple inheritance.." I died a little. It is not bad practice, it is just useful and powerful on its occasions. As with everything, apply with care. The fact that Java does not support it does not mean multiple inheritance is bad – TeaOverflow Oct 04 '14 at 18:06
  • 1
    +1, Nice question. It helps to understand many useful scenarios, that we should be taking care while programming Java. – nullptr Oct 04 '14 at 18:09
  • possible duplicate of [Interview Puzzle - Multiple Inheritance in Java?](http://stackoverflow.com/questions/24378375/interview-puzzle-multiple-inheritance-in-java) – Raedwald Jan 27 '15 at 08:06

4 Answers4

2

This is not multi-inheritance. Each class has exactly one direct super class. If your example was considered multi-inheritance, you wouldn't be able to use the extends keyword at all, since each class already extends by default the Object class.

Multi-inheritence would be

class C extends A,B {}

And that's illegal in Java.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 2
    Thanks a ton Oh so you mean we cannot do this Class A, Class B extends A, Class C extends A, Class C extends D ? – JavaQuest Oct 04 '14 at 18:00
  • 1
    @JavaQuest Ah, sorry I didn't see Class C extending more than one time. I apologize. Edited comment. Sorry you can't do that. Any sub class can extend from at most one any super class. – nullptr Oct 04 '14 at 18:02
  • 2
    No, you CAN'T have C extend both A and D. Not in Java. If one or both of them were interfaces, it could implement one or both. But all classes in Java extend exactly one class (Object if not specified). – arcy Oct 04 '14 at 18:03
2

Your code does not contain multiple inheritance, and is, indeed, legal Java syntax. Multiple inheritance refers to a case where a class directly extends two superclasses. For example:

public class MyClass extends MyFather, MyMother {
}

Note that this is, of course, illegal Java syntax.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

"Multiple inheritance" in Java basically means inheriting multiple interfaces, not inheriting multiple implementations.

Now, there is a new feature of Java 8 that allows you to do something like multiple inheritance of actual implementations, via interfaces and something called default methods. I would strongly encourage you to really master the basics of Java first before trying them. Once you are ready, here is a good tutorial on default methods.

sparc_spread
  • 10,643
  • 11
  • 45
  • 59
2

The code you have written above is an example of Multilevel inheritance not Multiple Inheritance.

Multiple Inheritance is like :

class A extends B,C {

//this code is not valid in JAVA

}

And, if you want to use interfaces for implementing a structure like multiple inheritance, then you could use:

interface test_interface1{
/*all the methods declared here in this interface should be the part   
** of the class which is implementing this current interface 
*/
}

Similarly :

interface test_interface2{

}

So, create a class TestClass like :

class TestClass implements test_interface1,test_interface2 {

//now you have to use each and every method(s) declared in both the interfaces
// i.e. test_interface1 & test_interface2

}

You could also use a syntax like:

class TestClass extends AnyClass implements test_interface1,test_interface2 {
/* but do keep in mind - use extends keyword before implements
** and now you know you cannot use more than 1 class names with extends keyword
** in java.
*/