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!!

3
  • 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 Commented Oct 4, 2014 at 18:06
  • 1
    +1, Nice question. It helps to understand many useful scenarios, that we should be taking care while programming Java. Commented Oct 4, 2014 at 18:09
  • possible duplicate of Interview Puzzle - Multiple Inheritance in Java? Commented Jan 27, 2015 at 8:06

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 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.
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).
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.

Comments

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.

1 Comment

I've never seen "Multiple inheritance" used to mean implementing multiple interfaces.
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.
*/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.