Documentation

The Java™ Tutorials
Hide TOC
Multiple Inheritance of State, Implementation, and Type状态、实现和类型的多重继承
Trail: Learning the Java Language
Lesson: Interfaces and Inheritance
Section: Inheritance

Multiple Inheritance of State, Implementation, and Type状态、实现和类型的多重继承

One significant difference between classes and interfaces is that classes can have fields whereas interfaces cannot. 类和接口之间的一个显著区别是类可以有字段,而接口不能。In addition, you can instantiate a class to create an object, which you cannot do with interfaces. 此外,您可以实例化一个类来创建一个对象,这是接口无法做到的。As explained in the section What Is an Object?, an object stores its state in fields, which are defined in classes. 什么是对象一节所述,对象将其状态存储在字段中,字段在类中定义。One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes. Java编程语言不允许扩展多个类的一个原因是为了避免状态的多重继承问题,即从多个类继承字段的能力。For example, suppose that you are able to define a new class that extends multiple classes. 例如,假设您能够定义一个扩展多个类的新类。When you create an object by instantiating that class, that object will inherit fields from all of the class's superclasses. 当您通过实例化该类来创建对象时,该对象将从该类的所有超类继承字段。What if methods or constructors from different superclasses instantiate the same field? 如果来自不同超类的方法或构造函数实例化同一个字段怎么办?Which method or constructor will take precedence? 哪个方法或构造函数优先?Because interfaces do not contain fields, you do not have to worry about problems that result from multiple inheritance of state.因为接口不包含字段,所以您不必担心由于状态的多重继承而导致的问题。

Multiple inheritance of implementation is the ability to inherit method definitions from multiple classes. 实现的多重继承是从多个类继承方法定义的能力。Problems arise with this type of multiple inheritance, such as name conflicts and ambiguity. 这种类型的多重继承会出现问题,例如名称冲突和歧义。When compilers of programming languages that support this type of multiple inheritance encounter superclasses that contain methods with the same name, they sometimes cannot determine which member or method to access or invoke. 当支持这种类型的多重继承的编程语言的编译器遇到包含同名方法的超类时,它们有时无法确定要访问或调用的成员或方法。In addition, a programmer can unwittingly introduce a name conflict by adding a new method to a superclass. 此外,程序员可以通过向超类添加新方法而无意中引入名称冲突。Default methods introduce one form of multiple inheritance of implementation. 默认方法引入了一种实现的多重继承形式。A class can implement more than one interface, which can contain default methods that have the same name. 一个类可以实现多个接口,这些接口可以包含具有相同名称的默认方法。The Java compiler provides some rules to determine which default method a particular class uses.Java编译器提供一些规则来确定特定类使用的默认方法。

The Java programming language supports multiple inheritance of type, which is the ability of a class to implement more than one interface. Java编程语言支持类型的多重继承,这是一个类实现多个接口的能力。An object can have multiple types: the type of its own class and the types of all the interfaces that the class implements. 一个对象可以有多种类型:它自己的类的类型和该类实现的所有接口的类型。This means that if a variable is declared to be the type of an interface, then its value can reference any object that is instantiated from any class that implements the interface. 这意味着,如果一个变量被声明为接口的类型,那么它的值可以引用从实现该接口的任何类实例化的任何对象。This is discussed in the section Using an Interface as a Type.这已在将接口用作类型一节中讨论。

As with multiple inheritance of implementation, a class can inherit different implementations of a method defined (as default or static) in the interfaces that it extends. 与实现的多重继承一样,类可以继承在其扩展的接口中定义的(默认或静态)方法的不同实现。In this case, the compiler or the user must decide which one to use.在这种情况下,编译器或用户必须决定使用哪一个。


Previous page: Inheritance
Next page: Overriding and Hiding Methods