开发者

How does multiple inheritance in Java work? [duplicate]

开发者 https://www.devze.com 2023-01-11 02:54 出处:网络
This question already has answers here: Java : If A extends B and B extends Object, is that multiple inheri开发者_运维技巧tance
This question already has answers here: Java : If A extends B and B extends Object, is that multiple inheri开发者_运维技巧tance (11 answers) Closed 7 years ago.

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?


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.


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.


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


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)


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消