且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java:如果A扩展B和B扩展Object,就是多重继承

更新时间:2023-09-23 21:03:16


我的回答是否正确?

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.

继承:从两个或多个没有is a关系的基础继承; (在Java中,因为 Object 始终是基数,它将是后者):

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):

(图片来源: http: //yuml.me inscruffymode)

(Image credits: http://yuml.me in "scruffy" mode)


内部会发生什么?

Internally What happens actually?

只是你说的:有多个级别。当编译器解析实例上的成员时:

Just what you said: There are multiple levels. When the compiler is resolving a member on an instance:

obj.member

...它看看是否类型 obj (在这种情况下是一个类, ClassB )具有成员,因为它直接提供它或通过继承提供它。在运行时,JVM使用该对象实际上具有的成员

...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.

我之所以说大部分是因为Java有接口,而从Java 8开始,它在接口上有默认方法。这会使事情变得复杂一点,但是你在关于 Object ClassA $ c> $ c>和 ClassB

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 是一个与两种不同类型的关系:它继承的类类型,以及它实现的几种接口类型中的任何一种。没有默认方法的接口不是以实用的方式进行多重继承(类必须提供实现),但是它们确实使得类可以具有来自不相关类型树的多个is a关系。 (我不是学术人员,有可能学术界会认为他们以学术方式提供多重继承。)

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 8,接口可以提供默认实现他们定义的方法,这真的模糊了线,即使在实践层面。让我们再深入一点:

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:

假设我们有 ClassA

class ClassA {
    void doSomething() {
        // Code here
    }
}

Interface1

interface Interface1 {
    default void doSomethingElse() { // Requires Java 8
        // Code here
    }
}

,最后 ClassB

class ClassB extends ClassA implements Interface1 {
}

ClassB 继承的执行 doSomething > ClassA 。但它 Interface1 获取 doSomethingElse 的默认版本。我们没有在 ClassB 中实现它,但 ClassB 不是抽象的:它真的有 doSomethingElse 。它从接口获取。我使用gets而不是继承在这里,但这看起来像很多像继承默认方法。

ClassB inherits the implementation of doSomething from ClassA. But it also gets 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 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:


  • 超级是? (Java 8的回答: ClassA

  • (Java 8的回答:单行构造函数链接,接口没有构造函数。)

  • 你运行多次继承的构造函数,不止一次? (Java 8的答案:你不能多次继承构造函数,接口不能有它们。)

  • 如果你继承多个方法具有相同的签名会发生什么? (Java 8的答案:如果其中之一来自基类,那就是所使用的基类;一个基类的实现可以覆盖多个接口的默认方法。如果你有多个默认方法,时间,它是一个编译时错误如果一个接口已经改变没有类被重新编译,并且情况在运行时出现,它是一个运行时 IncompatibleClassChangeError 异常列出冲突的默认方法。)

  • 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.)