简体   繁体   English

Java对多重继承的怀疑

[英]Java Doubt on Multiple inheritance

Java does not support multiple inheritance but Object class is by default super class of all classes. Java不支持多重继承,但Object类默认是所有类的超类。 eg 例如

class Object
{

}
class B
{

}
class A extends B
{
}

Class A can access all method of B and Object.Is not it a example of multiple inheritance? A类可以访问B和Object的所有方法。这不是多重继承的例子吗? So is it correct that Java does not support multiple inheritance. 所以Java不支持多重继承是正确的。


my question is not to find difference between multilevel and multiple inheritance. 我的问题是找不到多级和多重继承之间的区别。 Java Docs,it self says :Class Object is the root of the class hierarchy. Java Docs,它自称:Class Object是类层次结构的根。 Every class has Object as a super class. 每个类都将Object作为超类。 All objects, including arrays, implement the methods of this class. 所有对象(包括数组)都实现此类的方法。 So it means Class Object is super class of Class A{Previous example}. 所以它意味着Class Object是Class A {Previous example}的超类。 But Class B is also Super Class of Class A. SO what is meaning of it ? 但是B级也是A级的超级类别。那么它的含义是什么?

它被称为继承的 transitive 性质

Look at the difference between transitive inheritance ( C inherits directly from B and transitively from A ): 看看传递性继承之间的区别( C直接从B继承而传递性地从A继承):

及物

and multiple inheritance ( C inheriting from both A and B ): 多重继承( C继承自AB ):

及物

No it isn't. 不,不是。 Multiple inheritance is when a class has more than one direct base class, as in: 多重继承是指一个类有多个直接基类,如:

class A {}

class B {}

// not valid Java
class C extends A, B {}

A class may have many indirect base classes, each with only one direct base class, as in: 一个类可以有许多间接基类,每个类只有一个直接基类,如:

class D extends A {}

class E extends D {}

class F extends E {}

Here the inheritance hierarchy is F -> E -> D -> A -> Object, but this is still single inheritance. 这里的继承层次结构是F - > E - > D - > A - > Object,但这仍然是单继承。

Since no Java class can extend directly two or more classes you can safely say that Java does not support multiple inheritance. 由于没有Java类可以直接扩展两个或更多类,因此可以肯定地说Java不支持多重继承。

You would have multiple inheritance if you were able to say class A extends B, C , but you can never do that. 如果你能说A类扩展B,C你会有多重继承,但是你永远不能这样做。

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

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