How does multiple inheritance work in Java?

The Object class is the root of the class hierarchy. Each class has an Object as a superclass. So, if I extend the API class, will it be like multiple inheritance? Obviously, Java does not support multiple inheritance. How it works?

+4
source share
9 answers

A superclass is not the same as a parent class. You can have only one mother, but you have many more female ancestors.

+9
source

Java does not support multiple inheritance, as everyone else has explained.
But you can (sort of) have multiple inheritance when implementing multiple interfaces:

 interface Moveable { void relocate(Coordinate position); Coordinate getCurrentPos(); } interface Tradeable { void sell(BigInteger amount); void buy(BigInteger amount); } interface Crashable { void crash(); } class Vehicle implements Moveable, Tradeable, Crashable { } 

Vehicle should now have all the methods from the interfaces that it implements.

+6
source

No, Object will only be the final parent class of any class you create.

Multiple inheritance means that you can write a class that extends String and Integer , for example, and gets the properties of each. This is not possible with Java. You will probably want to see the delegate template if this is what you want to do

+4
source

no, simple inheritance as usual

 grandparent parent child 

the child has only one parent, and the parent has grandparents (does not make logical sense, but anything :)

multiple inheritance would be when you inherit from two different classes, do not have to do anything with each other

 donkey car donkeycar 

(as you already noted, its not possible in java)

+3
source

The superclass of your object also has a superclass, etc.

All objects form a tree with java.lang.Object, since it is the root node.

+2
source

Not.

Multiple inheritance means that you inherit, for example, from two classes

 class A {} class B {} class C extends A, B {} 

and this is not possible in Java.

What you can do is

 class A {} class B extends A {} class C extends B {} 

So you have more than one superclass, but only one parent.

+1
source

My understanding of this is that multiple inheritance works horizontally (several parent superclasses inherited directly into one subclass), and not vertically (parents of parents), thinking about the inheritance tree.

As you say, only vertical view is allowed in Java.

0
source

Multiple inheritance is excluded by Sun Microsystem. However, multiple inheritance is useful in some way. Here is a link that can help you with multiple java inheritance.

http://java.sys-con.com/node/37748

Hope this helps.

0
source

Inheritance is transitive. You extend the API class, and the API class itself extends Object. This way you indirectly extend Object. In short, if A continues to B and B continues to C, then A continues to C.

This is not multiple inheritance. The Inheritance Chain, if you like, maybe as long as you want it. But, in the end, in the whole object there is an object at the end.

Real multiple inheritance will immediately extend to two unrelated classes. You cannot do this in Java.

0
source

All Articles