How does multiple inheritance in Java work? [duplicate]

旧城冷巷雨未停 提交于 2019-12-21 05:12:07

问题


Class Object is the root of class hierarchy. Every class has Object as a superclass. So, if I am extending a API class, will it be like, multiple inheritance? Obviously, Java doesn't support multiple inheritance. How does it then work?


回答1:


Superclass is not the same thing as parent class. You can only have one mother, but you have a much larger number of female ancestors.




回答2:


Java doesn't support multiple inheritance, as everyone else explained.
But you can (kind of) have multiple inheritance when you implement 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 {

}

Now Vehicle should all methods from the interfaces it implements.




回答3:


No, Object will just be the eventual parent class of any class you create

Multiple inheritence would mean you could write a class that extends String and Integer for example, and gains the properties of each. This cannot be done with Java. You probably want to look at the Delegate pattern if this is the sort of thing you want to do




回答4:


no, its just inheritance, business as usual

grandparent

parent

child

the child only has one parent, and the parent has a grandparent (doesnt make logical sense, but whatever :)

multiple inheritance would be when you inherit from two different classes does not need to have anying do with each other

donkey      car

     donkeycar

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




回答5:


The super class of your object also has a super class and so on.

All objects form a tree with java.lang.Object as it's root node.




回答6:


No.

The multiple inheritance mean 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 then one super class but only one parent.




回答7:


My understanding of it it that multiple inheritance works horizontally (multiple parent superclasses inherited directly into one subclass) rather than vertically (parents of parents), thinking of the inheritance tree.

As you say, only the vertical kind is allowed in Java.




回答8:


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

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

Hope that helps.




回答9:


Inheritance is transitive. You extend an API class, and that API class itself extends Object. So you're indirectly extending Object. Shorter, if A extends B and B extends C, then A extends C.

This is not multiple inheritance. The 'inheritance chain', if you will, can be as long as you want it to be. But in the end, everything has Object at the end of it.

Real multiple inheritance would be extending from two unrelated classes at once. You cannot do this in Java.



来源:https://stackoverflow.com/questions/3448864/how-does-multiple-inheritance-in-java-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!