如何解释Java中的多重继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18328629/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:32:30  来源:igfitidea点击:

How to explain multiple inheritance in Java

java

提问by Sachin J

Actually the question was asking by one of the interviewer

其实这个问题是面试官问的

Que:How can you say that java is not supporting multiple inheritance? If Object classis a parent of all classes in java.

Que:你怎么能说java不支持多重继承?如果Object 类是 java 中所有类的父类。

I have no answer of that question.

我对那个问题没有答案。

That means no clear idea about java concepts :-(

这意味着对Java概念没有明确的认识:-(

Ex: if A extends B

例如:如果A 扩展 B

And here A is already extending Object class. right? Now how its works?

而这里 A 已经扩展了 Object 类。对?现在它是如何工作的?

Please share your answers..

请分享你的答案..

采纳答案by Merlyn Morgan-Graham

Multiple inheritance is about multiple-direct-inheritance.

多重继承是关于多重直接继承。

A single class class can't have two immediate parent classes. It can have a grandparent class, though.

一个类不能有两个直接的父类。不过,它可以有一个祖父母类。

A extends Band B extends C, is notthe same as A extends both B and C.

A extends BB extends C,一样A extends both B and C

The reason this is disallowed is for simplicity when you have a case like:

不允许这样做的原因是为了简单起见,当您遇到以下情况时:

A extends both B and C

B extends D

C extends D

If you had such a case, and then you had this code:

如果你有这样的情况,然后你有这个代码:

A a = new A();
a.someAbstractOrVirtualMethodOnD();

... are you talking about the Bimplementation of someAbstractOrVirtualMethodOnD(), or the Cimplementation of that same method? Which should get called? (Hint: there isn't a great answer)

...您是在谈论 的B实现someAbstractOrVirtualMethodOnD(),还是C相同方法的实现?应该调用哪个?(提示:没有很好的答案)

So, Java bans it.

所以,Java 禁止它。

Note, you can get something much like multiple inheritance if you implementmultiple interfaces. But since there is only one concrete implementation, there is no confusion as to what gets called.

请注意,如果您有implement多个接口,您可以获得类似于多重继承的东西。但是由于只有一个具体的实现,所以对于调用什么没有混淆。

回答by Hari Menon

Multiple inheritance is where a single class can extend from multiple classes. That is not possible in java. See here: http://en.wikipedia.org/wiki/Multiple_inheritance

多重继承是单个类可以从多个类扩展的地方。这在java中是不可能的。请参阅此处:http: //en.wikipedia.org/wiki/Multiple_inheritance

When you do class A extends Bin Java, then Aextends Bonly, and not Object. Bin turn extends Object(or whatever else, which will eventually extend object)

当您class A extends B使用 Java 时,则仅A扩展B,而不是Object. B反过来扩展Object(或其他任何东西,最终会扩展对象)

回答by sanbhat

Multiple inheritanceshould allow a class to inherit multiple parent classes. But Java doesn't allow this since it might create Diamond problem

多重继承应该允许一个类继承多个父类。但是 Java 不允许这样做,因为它可能会产生钻石问题

Regarding Objectclass being parent, and then having many classes in inheritance hierarchy, its termed as Multi level inheritance

关于Object类是父类,然后在继承层次结构中有许多类,它被称为多级继承

Sidenote:

边注:

C#allows multiple inheritance by interfaceby allowing child to implement multiple parent type's methods separately even though they have same signature

C#通过允许子级分别实现多个父类型的方法,即使它们具有相同的签名,也允许通过接口进行多重继承

回答by Abubakkar

Multiple inheritance means a single class can inherit from multiple classes. In other way, it can have multiple parent classes.

多重继承意味着一个类可以从多个类继承。换句话说,它可以有多个父类。

For Objectclass example cited by the interviewer, there are two possibilities:

对于Object面试官举的课堂例子,有两种可能:

  1. The interviewer himself is confused about multiple parent class(multiple inheritance) and multiple child class.

  2. Or he is trying to trick you using that question

  1. 面试官自己对多父类(多继承)和多子类很困惑。

  2. 或者他试图用那个问题来欺骗你

A parent class can have many child classes and that does not relate to multiple inheritance.

一个父类可以有多个子类,这与多重继承无关。

回答by Shuhail Kadavath

Please refer this : http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

请参考:http: //docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

In case of Object class to other classes its not actually multiple inheritance ... As said , its where a single class can extend from multiple classes ..

在 Object 类到其他类的情况下,它实际上不是多重继承......如上所述,单个类可以从多个类扩展......

回答by Lokesh

The only resemblence of mutiple inheritance in java is Interfaces. A class can implement multiple interfaces.

java中多继承的唯一相似之处是Interfaces. 一个类可以实现多个接口。

Objectclass is not an example of multiple inheritance. May be you misinterpreted the question.

Objectclass 不是多重继承的例子。可能你误解了这个问题。

回答by ProgramFOX

Have a look at this StackOverflow answer: https://stackoverflow.com/a/9790475/2619912

看看这个 StackOverflow 答案:https://stackoverflow.com/a/9790475/2619912

Your class that extends that other class, but it extends Object, too, so you're still in one line of inheritance, not two.

你的类扩展了另一个类,但它也扩展了 Object,所以你仍然在继承中,而不是两个。

回答by Suresh Atta

On the top of all to keep the language design simple

最重要的是保持语言设计简单

And the example from the blogI follow regularly.

以及 我经常关注的博客中的示例。

enter image description here

在此处输入图片说明

1)We have two classes Band Cinheriting from A.

1)我们有两个类BC继承自A

2)Assume that Band Care overriding an inherited method and they provide their own implementation.

2) 假设BC正在覆盖一个继承的方法,并且它们提供了自己的实现。

3) Now Dinherits from both Band Cdoing multiple inheritance. Dshould inherit that overridden method, which overridden method will be used? Will it be from Bor C?

3) 现在D继承了BC进行多重继承。D应该继承那个被覆盖的方法,会使用哪个被覆盖的方法?是来自B还是C

Here we have an ambiguity.

这里我们有歧义。

Any ways to overcome this we have interfacesand Multilevel inheritance.

任何克服这一点的方法我们都有接口多级继承

Edit :

编辑 :

And here A is already extending Object class.

That is never called as Multiple inheritance.That is called Multi level inheritance.

那永远不会被称为Multiple inheritance。那被称为Multi level inheritance.

In Multi level ,

在多层次,

Many classes are involved in inheritance, but one class extends only one. The lowest subclasscan make use of all its super classes' contents.

许多类都涉及inheritance,但只有一个类扩展one。最低的subclass可以使用它的所有super classes内容。

回答by user2668376

A java class can only be a direct child of a single parent class. It can have a grandparent but no second parent. It is like having a single biological mother. You cannot have more than one biological mothers, sure you can have a grandmother.

java 类只能是单个父类的直接子类。它可以有一个祖父母,但没有第二个父母。这就像有一个单亲生母。你不能有一个以上的亲生母亲,当然你可以有一个祖母。

回答by Kapil

Let there be a class A and class B. Now we define a class Derived. Multiple Inheritance means: class Derived can inherit both class A and class B. But this is not possible in Java. Hence it does not support Multiple Inheritance.

假设有一个类 A 和类 B。现在我们定义一个类 Derived。多重继承意味着:类Derived可以同时继承A类和B类。但这在Java中是不可能的。因此它不支持多重继承。

回答by Juned Ahsan

The answer is Java supports multi-level inheritance but not multiple inheritance.

答案是 Java 支持多级继承,但不支持多重继承。