Java中的多继承? [英] Multiple inheritance in Java or not?

查看:101
本文介绍了Java中的多继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说,Java语言仅支持单继承。
但是,如何可以同时继承Object和任何其他类呢?不是多重继承。

It is said, that Java language support single inheritance only. However how is it possible to inherit from Object and from any other class at the same time? Isn't that a multiple inheritance.

其次,我们需要继承所有11个Object方法呢?我几乎不能想象为什么我需要它在I / O例如。

Secondly, what for do we need to inherit all 11 Object methods? I could hardly imagine why do I need it them in I/O e.g.

最后,JDK 8将为我们提供接口中的默认方法实现,以及如果这可能会导致Java中的多重继承。

Finally JDK 8 is going to offer us default methods realization in interfaces and if which would probably cause multiple inheritance in Java.

如果接口A提供方法a()具有默认实现,接口B提供另一个默认实现的a()方法,而我们的自定义类C实现两个接口和依赖默认实现 - 不会是死亡钻石

What if interface A provides method a() with default realization and interface B provides also a() method with another default realization and our custom class C implements both interfaces and rely on default realization - wouldn't that be Diamond of Death ?

推荐答案


但是,如何继承Object和任何其他
类与此同时?这不是一个多重继承。

However how is it possible to inherit from Object and from any other class at the same time? Isn't that a multiple inheritance.

这不是什么事情。不是所有的类都直接从 Object 类继承。但是只有在继承层次结构的顶层的类从 Object 类(隐含地)延伸。其余的类在层次结构中较低,从 Object 类通过超类延伸。

No this is not what happens. Not all classes directly extend from Object class. But only the class at the top level of inheritance hierarchy extends from Object class(implicitly). Rest of the classes lower in the hierarchy, extends from the Object class through the super classes. And, this is what we call multi-level inheritance.

所以,考虑下面的层次结构: -

So, consider the below hierarchy: -

class A { }

class B extends A { }

在上面的例子中, A类等效于 class A extends Object

In the above case, class A is equivalent to class A extends Object.


其次,我们需要继承所有11个Object方法? I
几乎不可能想象为什么我需要它在I / O

Secondly, what for do we need to inherit all 11 Object methods? I could hardly imagine why do I need it them in I/O

我怀疑你的意思是 override ,当你说继承。您不需要覆盖 Object 类的任何方法。它只是在你的要求,是否重写任何方法或不。例如: - 你通常要覆盖 equals()方法,以便为你的实例编写自定义等同测试。在这种情况下,您还应该覆盖 hashCode()方法,以保持 equals() hashCode()

I suspect you meant override when you say inherit. You don't need to override any method of Object class. It's just on your requirement, whether to override any method or not. For e.g.: - You would often want to override equals() method, so as to write custom equality test for your instances. And in that case, you should also override the hashCode() method, to maintain the contract of equals() and hashCode().


最后,JDK 8将为我们提供默认方法实现在
接口和如果这可能会导致多个继承在
Java。

Finally JDK 8 is going to offer us default methods realization in interfaces and if which would probably cause multiple inheritance in Java.

如果接口A提供方法a b $ b interface B还提供了另一个默认实现
的a()方法,我们的自定义类C实现了两个接口,并依赖默认
实现 - 不是钻石的死亡?

What if interface A provides method a() with default realization and interface B provides also a() method with another default realization and our custom class C implements both interfaces and rely on default realization - wouldn't that be Diamond of Death ?

我不能评论这个概念,因为我还没有读过这个东西。可能,我稍后会更新答案。

I can't comment on this concept, because I haven't read about this thing yet. Probably, I would update the answer sometime later.

这篇关于Java中的多继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆