简体   繁体   English

默认情况下,任何类都扩展Object类。 这不是说Java支持多重继承吗?

[英]By default any class extends Object class. Doesn't it mean java supports multiple inheritance?

Everybody knows that in java we can only extend "ONE" class. 众所周知,在Java中我们只能扩展“ ONE”类。

But for the sake of understanding: 但是为了理解:

  1. Any Java class implicitly extends java.lang.Object 任何Java类都隐式扩展了java.lang.Object
  2. If class A extends class B, wouldn't class A extend both class B and java.lang.Object implicitly ? 如果类A扩展了类B,则类A不会隐式扩展类B和java.lang.Object吗?

In such a case we are extending two classes by default. 在这种情况下,默认情况下我们将扩展两个类。

Why is it allowed if Java doesn't support multiple inheritance ? 如果Java不支持多重继承,为什么允许?

No, Java prevents a class from directly extending more than one super class. 不,Java阻止一个类直接扩展多个超类。 Class A can extend a class B which extends class C. This is still single inheritance. A类可以扩展B类,而B类可以扩展C类。这仍然是单一继承。 All the classes form a tree, where the root is the Object class, and each class (except of Object) has exactly one direct super-class (or parent class), which is either Object or some other class. 所有这些类都形成一棵树,其根是Object类,并且每个类(Object除外)都具有一个直接的超类(或父类),即Object或其他类。

That would be a multilevel inheritance . 那将是一个多层次的继承 You are mistaking multiple to multilevel. 您误会了多级到多级。

A->B->C //This is multilevel inheritance which you are talking about

Multiple inheritance is like (which is not possible in java) 多重继承就像(在Java中是不可能的)

     A
   |   |
   B   C

Java doesn't support multiple inheritance that makes any ambiguous cases to fade away. Java不支持使任何模棱两可的情况消失的多重继承。 But careful implementation of implement keyword for implementing does give feel of multiple inheritance 但是认真实现的实现关键字确实会带来多重继承的感觉

Conclusion: 结论:

Class A can extend a class B which extends class C. This is still single inheritance. A类可以扩展B类,而B类可以扩展C类。这仍然是单一继承。 All the classes form a tree, where the root is the Object class, and each class (except of Object) has exactly one direct super-class (or parent class) 所有类都形成一棵树,其中根是Object类,并且每个类(Object除外)都只有一个直接的超类(或父类)

每当类A扩展类B时,类A不再扩展Java.lang.Object,但是:由于每个类都扩展了Java.lang.Object,如您之前所说,类B将扩展Java.lang.Object,因此类A是扩展Java.lang.Object。仍然是该特定类的子类。

"Implicitly extends Object by default" means that if you don't see the extends keyword in the class declaration it "invisibly but directly" extends Object. “默认情况下,隐式扩展对象”意味着如果您在类声明中没有看到extend关键字,则它“无形但直接”扩展了Object。 If you see the extends keyword, the class does not directly extend Object, but the class mentioned in the extends clause. 如果看到extends关键字,则该类不会直接扩展Object,而是会在extends子句中提到的类。 Now you have to traverse that hierarchy and at one point, you'll find a parent class with no "extends", and there the implicit inheritance strikes again. 现在您必须遍历该层次结构,并在某一时刻找到一个没有“扩展”的父类,然后隐式继承再次出现。

All classes in a hierarchy transitively extend Object, but only the root does it directly. 层次结构中的所有类都可传递地扩展Object,但只有根直接进行扩展。 Transitively because all subclasses inherit all the traits of their parents, including their parent classes and implemented interfaces. 可传递的,因为所有子类都继承其父级的所有特征,包括其父级类和已实现的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM