问题
We say that java follows a single inheritance model i.e. a Java class can extend only one class at max. and then say that every java class is inherited from Object class.
Suppose there are two classes A and B. Both A and B extend from Object. Now suppose A extends B. Doesn't it imply that A has multiple inheritence (A is inheriting from both B and Class Object)?
回答1:
Look at the difference between transitive inheritance (C inherits directly from B and transitively from A):
and multiple inheritance (C inheriting from both A and B):
Everything is just added on, except methods with the same signature, which are overridden. Even variables declared with the same name are added on, they're just said to be "hidden" but can still be accessed using casting, or the super keyword if it's the immediate parent.
回答2:
Excepting Object, which has no superclass, every class has one and |
only one direct superclass (single inheritance). In the absence of any
other explicit superclass, every class is implicitly a subclass of Object.
in your question A directly inherit B and Object class is inherited by B not by A..This is called transitive inheritance
回答3:
You can have an inheritance chain, this is not multiple inheritance. You cannot have a class that inherits from more than one class at a time.
Forbidden by the language:
class A extends B, C
{}
回答4:
Multiple Inheritance: (Not supported by Java)
Class A extends Object
Class B extends Object
Class C extends A, B
Object
/\
/ \
/ \
A B (Not supported by Java)
\ /
\ /
\ /
C
Multilevel/Transitive Inheritance:(Supported by Java)
Class B extends Object
Class A extedns B
Object
|
|
|
B (Supported by Java)
|
|
|
A
回答5:
A Java class can only directly inherit from one class. In this case, A doesn't directly inherit from Object, only from A.
Are you solely asking from the perspective of terminology, or is there some behaviour you're interested in?
回答6:
Object A extends object B which extends Object. It's not multiple inheritance, it's hierarchy
回答7:
The main problems with multiple inheritance is the ambiguity, when two classes define the same method and are being both overriden and the diamond problem. Java doesn't suffer from this as, it allows overriding only a single class. This obviously refers to direct overriding and doesn't deal with hierarchies of inheritance. This kind of inheritance (A overring B, B overriding and so on) isn't affected by any problems.
回答8:
In Java, each class can directly extend only from one parent class. So, either B extends from Object and A extends from B, xor both A and B extend from Object.
You are supposing that both is true at the same time - this is not possible in Java.
If B extends from Object, and A extends from B, then yes, A does inherit from Object, but there is still a single parent to each and every class:
class Object
|
+-- class B
|
+-- class A
In other words, Java doesn't support multiple inheritance.
回答9:
Java doesn't let you extend more than 1 super-class. However, a class can implement multiple interfaces. You can create a structure of interfaces and abstract classes that let's you hack extending more than 1 class. Good luck!
来源:https://stackoverflow.com/questions/9192689/inheritance-in-java-language