简体   繁体   English

Java:如果A扩展了B并且B扩展了Object,那是多重继承

[英]Java : If A extends B and B extends Object, is that multiple inheritance

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. 那么您可以说A类扩展了B类和Object类,这意味着它是多重继承。 So how can you say Java does not support multiple inheritance? 那么,怎么说Java不支持多重继承呢?

Me - Actually class B extends class Object, so when you extend class B in class A then class A extends class Object indirectly. -实际上,类B扩展了类Object,因此,当您在类A中扩展类B时,类A间接扩展了类Object。 This is multi-level inheritance, not multiple inheritance. 这是多级继承,而不是多继承。

But my answer did not satisfy him. 但是我的回答使他不满意。

Is my answer correct? 我的答案正确吗? Or where am I wrong? 还是我在哪里错? What actually happens internally? 内部实际发生了什么?

My answer is correct? 我的答案是正确的吗?

Yes, mostly, and certainly in the context you describe. 是的,大多数情况下,当然也可以根据您所描述的情况。 This is not multiple inheritance: 这不是多重继承:

对象^-ClassA ^-ClassB

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; 这是多重继承:从两个或多个彼此没有任何“ is”关系的碱基继承; 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始终是基础,因此它将是后者):

对象^-ClassA,对象^-ClassB,ClassA ^-ClassC,ClassB ^-ClassC

(Image credits: http://yuml.me in "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. ...它看起来是因为obj的类型(在这种情况下是一个类,例如ClassB )是否具有member ,这是因为它直接提供了它,还是通过继承了它。 At runtime, the JVM uses the member the object actually has. 在运行时,JVM使用对象实际拥有的member


The reason I said "mostly" above is that Java has interfaces, and as of Java 8 it has "default methods" on interfaces. 我上面说“主要”的原因是Java具有接口,而从Java 8开始,它在接口上具有“默认方法”。 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 . 这使事情有些复杂,但是在描述访调员对ObjectClassAClassB描述时,有关级别的答案是正确的。

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 . 在Java中,接口始终使某种东西具有两种不同类型的“是”关系成为可能:它继承自一个类类型,并且实现了几种接口类型中的任何一种。 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.) (我不是学术人员,学术人员可能会争辩说他们以学术方式提供多重继承。)

With Java 8, interfaces can provide default implementations of the methods they define, which really blurs the lines even at the practical level. 使用Java 8,接口可以提供它们定义的方法的默认实现,即使在实际水平上,这也确实使界限模糊了。 Let's look at that a bit more deeply: 让我们更深入地看一下:

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 . ClassBClassA继承了doSomething的实现。 But it also gets the "default" version of doSomethingElse from Interface1 . 但是它Interface1获得doSomethingElse的“默认”版本。 We didn't implement it in ClassB , but ClassB isn't abstract: It really has doSomethingElse . 我们没有在ClassB实现它,但是ClassB不是抽象的:它确实具有doSomethingElse It gets it from the interface . 它从接口获取它。 I used the word "gets" rather than "inherits" there, but this looks a lot like inheriting the default method. 我用这个词“被”,而不是“继承”有,但像继承的默认方法,这看起来很多

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? super类型应该是什么? (Java 8's answer: ClassA ) (Java 8的答案: ClassA
  • What order do you run constructors in? 您以什么顺序运行构造函数? (Java 8's answer: Single-lineage constructor chaining, interfaces don't have constructors.) (Java 8的答案:单谱系构造函数链接,接口没有构造函数。)
  • 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.) (Java 8的答案:您不能多次继承构造函数,接口没有它们。)
  • 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.) (Java 8的回答:如果其中之一来自基类,那就是使用的那个;基类的实现可以覆盖多个接口的默认方法。如果您在编译时具有来自不同接口的具有相同签名的多个默认方法,如果在不重新编译类的情况下更改了接口,并且在运行时出现这种情况,则是运行时IncompatibleClassChangeError异常,列出了冲突的默认方法。)

you are correct 你是对的

First of all, Object class is the super/base/parent class of every class including user-defined classes. 首先,对象类是包括用户定义类在内的每个类的上级/基类/父类。

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文档以进行继承。

My answer is correct? 我的答案是正确的吗?

You are absolutely correct in saying that it is multi-level inheritance and not multiple inheritance. 绝对正确地说这是多级继承,而不是多重继承。

Only the root of the hierarchy is Object , all classes don't individually 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 ,则将在A a = new A();上调用Object构造函数多少次A a = new A();

The answer is only once, and that will be for the root of the hierarchy. 答案只有一次,这将是层次结构的根源。

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? 怎么样?

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 该类和接口可以实现许多接口,但只能扩展一个类

Your answer is correct ! 您的回答是正确的!

class Object //for illustration purpose
{
}

class B
{
}

class A extends B
{
}

When you create an object of class A, constructor chaining happens. 创建类A的对象时,会发生构造函数链接 ie 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的构造函数隐式调用super() ,因此,类B的构造函数被调用,然后隐式调用其超类,即Object类。

In java, a class extends only a single class because the constructor of that class only call one super class constructor. 在Java中,一个类仅扩展一个类,因为该类的构造函数仅调用一个超类构造函数。 This is not true in case of Interfaces since they do not have constructors. 对于接口,这是不正确的,因为它们没有构造函数。

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的构造函数。

Your answer is right, because java doesn't support multiple inheritance from classes. 您的答案是正确的,因为Java不支持从类进行多重继承。 Java supports multiple inheritance from interfaces, and there is no any other inheritance. Java支持从接口的多重继承,并且没有任何其他继承。 But you can use composition of classes, but that's another story. 但是您可以使用类的组合,但这是另一回事了。

Your answer is perfectly alright. 您的回答完全正确。 You can explain interms of multilevel inheritance support from Object class in java 您可以从Java中的Object类解释多级继承支持的术语

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 :) 好吧,也许我不会因为打电话给面试官的问题而愚蠢:)

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

The answer you gave is correct. 您给出的答案是正确的。 The interviewer was wrong: 面试官错了:

Internal process 内部流程

if suppose Class A Doesn't extends any other class 
then ---> Class B extends java.lang.Object 
then ---> Class A extends B
then class A also inherited the property of java 'Object' class...

so,Java doesn't support multiple inheritance. 因此,Java不支持多重继承。

If you want to verify this process just generate 'javadoc' for your class A and verify the results. 如果要验证此过程,只需为类A生成“ javadoc”并验证结果。

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

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