简体   繁体   English

多重继承和类对象

[英]Multiple Inheritance and class Object

I am pretty new to OOP. 我是OOP的新手。 We all know that Java does not support multiple inheritance; 我们都知道Java不支持多重继承; however, all Java classes inherit from Object and can also inherit from another class. 但是,所有Java类都继承自Object ,并且还可以从另一个类继承。 Can we consider this as multiple inheritance? 我们可以将其视为多重继承吗? How does Java handle such a thing? Java如何处理这样的事情?

It's not multiple inheritance it's multi level inheritance. 它不是多重继承,而是多级继承。 Classes can extend one other class, which can extend one other class, ..., which ultimately extends Object: 类可以扩展另一个类,它可以扩展另一个类,...,最终扩展Object:

A --> B --> C --> Object

Multiple inheritance would be 多重继承

A ----> B 
  \
   \--> C

This means that when a method or a field is used inside A, it's looked up in A, then in B, then in C, then in Object. 这意味着当在A中使用方法或字段时,它在A中查找,然后在B中查找,然后在C中查找,然后在Object中查找。

With multiple inheritance, it would have to be looked up in A, then in B and C, and there could be a conflict because the same method or field could exist in both superclasses. 对于多重继承,必须在A中查找,然后在B和C中查找,并且可能存在冲突,因为两个超类中可能存在相同的方法或字段。

That is not multiple inheritance ....That is multi level inheritance in java 这不是多重继承....这是java中的多级继承

Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). Excepting Object,没有超类,每个类都有一个且只有一个直接超类(单继承)。 In the absence of any other explicit superclass, every class is implicitly a subclass of Object. 在没有任何其他显式超类的情况下,每个类都隐式地是Object的子类。

在此输入图像描述

Your class that extends that other class, but it extends Object, too, so you're still in one line of inheritance, not multiple inheritance. 你的类扩展了其他类,但它也扩展了Object,所以你仍然在一行继承,而不是多重继承。

It calls as a multi-level inheritance. 它称为多级继承。 not multiple inheritance. 不是多重继承。

Its still multi level inheritance. 它仍然是多级继承。 If u would use multiple inheritance in java - use intefrace for these purposes. 如果你在java中使用多重继承 - 使用intefrace用于这些目的。

Because: 因为:

Java support only multiple interface inheritance, and java does not support multiple inheritance

what you explain is multilevel inheritance that allowed in java. 你解释的是java中允许的多级继承

在此输入图像描述

but multiple inheritance not allowed in java. 但java中不允许多重继承

在此输入图像描述

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

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