-2

While i was working on my assignment on Java, I created 3 classes, lets say class 1, class2 and class3

Then I tried to define one of the classes in the following way:

public class class1 extends class2 extends class3{

To be honest, I was almost sure it would not work and in fact did not.

So I wanted to know If I could create a single subclass for two different super classes. Thank You!

user3519322
  • 69
  • 1
  • 1
  • 7

3 Answers3

3

What you are asking for is multiple inheritance. Java does not support this.

Multiple inheritance is hard for compiler writers to implement - mainly because of the diamond problem.

Except - in Java 8 there are interfaces with default methods. This allows sort of a light-weight version of multiple inheritance.

tbsalling
  • 4,477
  • 4
  • 30
  • 51
0

No you can't. In Java you can extend only one base class. You can however implement from multiple interfaces

Harry
  • 1,362
  • 12
  • 19
0

For java,no.

in "Tink in java"1.8 Single Root extends structure has mentioned that.

but in C++,yes.

Kevin
  • 31
  • 2