Java : 如果 A 扩展 B 和 B 扩展对象,是多重继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24378375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java : If A extends B and B extends Object, is that multiple inheritance
提问by Ankit Sharma
I just had an interview, and I was asked a question.
刚面试完,问了一个问题。
Interviewer- Does Java support multiple inheritance?
面试官- Java 是否支持多重继承?
Me- No
我- 没有
Interviewer- Each class in Java extends class Object (except class Object) and if we externally extend one class like
面试官- Java 中的每个类都扩展了类 Object(类 Object 除外),如果我们从外部扩展一个类,例如
Class A extends B{
// some code here
}
then you can say that class A extend class B and class Object, which means it is multiple inheritance. So how can you say Java does not support multiple inheritance?
那么你可以说A类继承了B类和Object类,也就是说它是多重继承的。那么你怎么能说Java不支持多重继承呢?
Me- Actually class B extends class Object, so when you extend class B in class A then class A extends class Object indirectly. This is multi-level inheritance, not multiple inheritance.
我- 实际上 B 类扩展了类 Object,所以当你在 A 类中扩展 B 类时,A 类间接扩展了类 Object。这是多级继承,不是多重继承。
But my answer did not satisfy him.
但我的回答并没有让他满意。
Is my answer correct? Or where am I wrong? What actually happens internally?
我的回答正确吗?或者我错在哪里?内部实际发生了什么?
采纳答案by T.J. Crowder
My answer is correct?
我的回答正确吗?
Yes, mostly, and certainly in the context you describe. This is not multiple inheritance:
是的,大多数情况下,当然在您描述的上下文中。这不是多重继承:
It's what you said it is, single inheritance with multiple levels.
这就是你所说的,多层次的单一继承。
This is multiple inheritance: Inheriting from two or more bases that don't have any "is a" relationship with each other; that would be inheriting from unrelated lines, or from lines that had previously diverged (in Java, since Object
is always a base, it would be the latter):
这是多重继承:从两个或多个彼此没有任何“是”关系的碱基继承;这将继承自不相关的行,或从以前发散的行(在 Java 中,因为Object
始终是基础,所以将是后者):
(Image credits: http://yuml.mein "scruffy" mode)
(图片来源:http: //yuml.me在“邋遢”模式下)
Internally What happens actually?
内部实际发生了什么?
Just what you said: There are multiple levels. When the compiler is resolving a member on an instance:
正如您所说:有多个级别。当编译器解析实例上的成员时:
obj.member
...it looks to see if the type of obj
(which in this case is a class, say ClassB
) has member
, either because it provides it directly or it has it through inheritance. At runtime, the JVM uses the member
the object actually has.
...它查看的类型obj
(在这种情况下是一个类,比如说ClassB
)是否具有member
,要么是因为它直接提供它,要么是通过继承获得它。在运行时,JVM 使用member
实际拥有的对象。
The reason I said "mostly" above is that Java has interfaces, and as of Java 8 it has "default methods" on interfaces. This complicates things a bit, but your answer about levels is correct in the context of what you described the interviewer saying about Object
, ClassA
, and ClassB
.
我在上面说“主要”的原因是 Java 有接口,从 Java 8 开始,它在接口上有“默认方法”。这会使事情变得复杂了一点,但你对各级答案是你所描述的面试官说关于上下文正确Object
,ClassA
和ClassB
。
Interfaces have always made it possible, in Java, for something to have an "is a" relationship with two different types: A class type it inherits from, and any of several interface types it implements. Interfaces without default methods aren't multiple inheritance in a practical way (the class has to provide the implementation), but they did make it possible for a class to have multiple "is a" relationships from unrelated type trees. (I'm not an academic, it's possible an academic would argue that they provide multiple inheritance in an academic way.)
在 Java 中,接口总是使某些事物与两种不同类型具有“是”关系成为可能:它继承自的类类型,以及它实现的几种接口类型中的任何一种。没有默认方法的接口实际上不是多重继承(类必须提供实现),但它们确实使一个类可以从不相关的类型树中具有多个“是一个”关系。(我不是学者,学者可能会争辩说他们以学术方式提供多重继承。)
With Java 8, interfaces can provide default implementations of the methods they define, which really blurs the lines even at the practical level. Let's look at that a bit more deeply:
在 Java 8 中,接口可以提供它们定义的方法的默认实现,这甚至在实用级别也确实模糊了界限。让我们更深入地了解一下:
Say we have ClassA
:
说我们有ClassA
:
class ClassA {
void doSomething() {
// Code here
}
}
and Interface1
:
和Interface1
:
interface Interface1 {
default void doSomethingElse() { // Requires Java 8
// Code here
}
}
and finally ClassB
:
最后ClassB
:
class ClassB extends ClassA implements Interface1 {
}
ClassB
inherits the implementation of doSomething
from ClassA
. But it alsogets the "default" version of doSomethingElse
from Interface1
. We didn't implement it in ClassB
, but ClassB
isn't abstract: It really has doSomethingElse
. It gets it from the interface. I used the word "gets" rather than "inherits" there, but this looks a lotlike inheriting the default method.
ClassB
继承doSomething
from的实现ClassA
。但它也获得了doSomethingElse
from的“默认”版本Interface1
。我们没有在 中实现它ClassB
,但ClassB
不是抽象的:它确实有doSomethingElse
. 它从界面中获取。我用这个词“被”,而不是“继承”有,但这个看上去一个很多像继承的默认方法。
This is basically multiple-inheritance "light" (as in "light beer"). It does an end-run around the thornier problems with true multiple inheritance, like:
这基本上是多重继承的“光”(如“淡啤酒”)。它最终解决了真正的多重继承的棘手问题,例如:
- What should the type of
super
be? (Java 8's answer:ClassA
) - What order do you run constructors in? (Java 8's answer: Single-lineage constructor chaining, interfaces don't have constructors.)
- Do you run constructors that you inherit more than once, more than once? (Java 8's answer: You can't inherit constructors more than once, interfaces don't have them.)
- What happens if you inherit multiple methods with the same signature? (Java 8's answer: If one of them is from the base class, that's the one that's used; a base class's implementation can override the default method of multiple interfaces. If you have multiple default methods with the same signature from different interfaces at compile-time, it's a compile-time error. If an interface has been changed without the class being recompiled and the situation arises at runtime, it's a runtime
IncompatibleClassChangeError
exception listing the conflicting default methods.)
- 应该是什么类型
super
?(Java的8的答案:ClassA
) - 你以什么顺序运行构造函数?(Java 8 的答案:单谱系构造函数链,接口没有构造函数。)
- 您是否运行多次继承的构造函数?(Java 8 的答案:您不能多次继承构造函数,接口没有它们。)
- 如果继承多个具有相同签名的方法会发生什么?(Java 8 的回答:如果其中一个来自基类,那就是使用的那个;基类的实现可以覆盖多个接口的默认方法。如果您在编译时有多个来自不同接口的具有相同签名的默认方法 -时间,这是一个编译时错误。如果在没有重新编译类的情况下更改了接口,并且这种情况在运行时出现,则是一个运行时
IncompatibleClassChangeError
异常,列出了冲突的默认方法。)
回答by FruitDealer
Your answer is right, because java doesn't support multiple inheritance from classes. Java supports multiple inheritance from interfaces, and there is no any other inheritance. But you can use composition of classes, but that's another story.
您的答案是正确的,因为 java 不支持类的多重继承。Java 支持从接口的多重继承,并且没有任何其他继承。但是您可以使用类的组合,但那是另一回事。
回答by Siranjeevi Rajendran
Your answer is perfectly alright. You can explain interms of multilevel inheritance support from Object class in java
你的回答完全没问题。你可以解释java中Object类的多级继承支持
回答by mohamed sulibi
Does java support multiple inheritance?
Yes for interfaces but not for classes.
是的接口但不是类。
The class and interface can implements many interfaces but extends only one class
类和接口可以实现多个接口但只扩展一个类
回答by Narendra Pathai
My answer is correct?
我的回答正确吗?
You are absolutely correctin saying that it is multi-level inheritance and not multiple inheritance.
你说它是多级继承而不是多重继承是绝对正确的。
Only the rootof the hierarchy is Object
, all classes don'tindividually extend Object.
只有层次结构的根是Object
,所有类都不会单独扩展 Object。
A counter to the interviewer:
对面试官的反驳:
If all classes extend Object
, then how many times constructor of Object
will be called on A a = new A();
如果所有类都扩展Object
,那么Object
将调用多少次的构造函数A a = new A();
The answer is only once, and that will be for the root of the hierarchy.
答案只有一次,而且是针对层次结构的根。
回答by Alexandre Santos
Yes, you are correct... as many others have pointed out. I just wanted to say that interviews are not only about technical knowledge, it is also about sticking to your guns. Some interviewers will question your answer, not because they want to know if you are sure of your convictions but also to test how well you can teach others and how well you handle an authoritative figure.
是的,你是对的……正如许多其他人指出的那样。我只想说,面试不仅是关于技术知识,也是关于坚持你的枪。一些面试官会质疑你的答案,不是因为他们想知道你是否确信自己的信念,而是想测试你能教给别人多少以及你如何处理权威人物。
For the first point, if you can't teach others then you can't be a mentor. Nowadays it is crucial to hire someone who can coach junior developers.... because it makes sense economically.
对于第一点,如果你不能教别人,那么你就不能成为导师。如今,聘请可以指导初级开发人员的人至关重要......因为这在经济上是有意义的。
For the second point, because they don't want you changing technical aspects just because your boss asked you to. If your boss asks you to remove all indexes from the database because they take up too much space, would you do it? Would you try to convince your boss otherwise? How?
第二点,因为他们不希望你仅仅因为你的老板要求你改变技术方面。如果您的老板要求您从数据库中删除所有索引,因为它们占用了太多空间,您会这样做吗?否则你会试图说服你的老板吗?如何?
回答by Santosh Pavate
Your answer is correct !
你的回答是正确的!
class Object //for illustration purpose
{
}
class B
{
}
class A extends B
{
}
When you create an object of class A, constructor chaininghappens.
i.e. the constructor of class A calls super()
implicitly and hence the constructor of class B is invoked, which then calls its super class implicitly which is the Object class.
当您创建类 A 的对象时,会发生构造函数链接。即类 A 的构造函数super()
隐式调用,因此调用类 B 的构造函数,然后隐式调用其超类,即 Object 类。
In java, a class extends only a single class because the constructor of that class only call one super class constructor. This is not true in case of Interfaces since they do not have constructors.
在java中,一个类只扩展一个类,因为该类的构造函数只调用一个超类构造函数。对于接口,情况并非如此,因为它们没有构造函数。
Also when an object of class A is created, and assume that you have defined the constructors of both classes A and B, then constructor of class B is executed first and then the constructor of class A.
同样,当创建了类 A 的对象时,假设您已经定义了类 A 和 B 的构造函数,则首先执行类 B 的构造函数,然后执行类 A 的构造函数。
回答by MSR
you are correct
你是对的
First of all, Object class is the super/base/parent class of every class including user-defined classes.
首先,Object类是包括用户自定义类在内的每个类的超/基/父类。
So even if we don't mention it explicitly, the user-defined classes extends Object class by default.
所以即使我们没有明确提到它,用户定义的类默认扩展了 Object 类。
its like
就像是
class A
class B extends A
but compiler read it as
class A extends Object
class B extends A
proved
证实
for more detail check this java documentation for inheritance
有关更多详细信息,请查看此java 文档的继承
回答by Cindy Langdon
What a dumb question.
多么愚蠢的问题。
Of course Java doesn't support multiple inheritance, and interfaces are not inherited.
当然Java不支持多重继承,接口也不是继承的。
Inheritance only happens via "extends", not via "implements". When you define a class implements several interfaces you are not saying it will be an extension of those interfaces, but it will have the same behavior, and behavior (at least in Java), doesn't define inheritance.
继承仅通过“扩展”发生,而不是通过“实现”发生。当您定义一个类实现多个接口时,您并不是说它将是这些接口的扩展,但它具有相同的行为,并且行为(至少在 Java 中)没有定义继承。
For Java to support multiple inheritance, it would need to support something like
为了让 Java 支持多重继承,它需要支持类似的东西
public class MI extends Object, MyOtherClass
Which Java can't.
哪个Java不能。
Well, maybe I wouldn't get the job for calling the interviewer's question dumb :)
好吧,也许我不会因为将面试官的问题称为愚蠢而得到这份工作:)
回答by Anurag Sharma
Your answer is absolutely correct.
你的回答是完全正确的。
These types of questions asked just to check whether a candidate is conceptually strong or not.
这些类型的问题只是为了检查候选人在概念上是否强大。
Well the simplest and precise answer to this question is here:
那么这个问题最简单和准确的答案是在这里:
"Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object."
"类可以派生自从类派生的类,依此类推,并最终从最顶层的类 Object 派生。这样的类据说是继承链中所有类的后代。反对。”
Please refer this link https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
请参考此链接 https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html