Guide to Inheritance in Java – Java中的继承指南

最后修改: 2018年 3月 3日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

One of the core principles of Object-Oriented Programming – inheritance – enables us to reuse existing code or extend an existing type.

面向对象编程的核心原则之一–继承–使我们能够重用现有代码或扩展现有类型。

Simply put, in Java, a class can inherit another class and multiple interfaces, while an interface can inherit other interfaces.

简单地说,在Java中,一个类可以继承另一个类和多个接口,而一个接口可以继承其他接口。

In this article, we’ll start with the need for inheritance, moving to how inheritance works with classes and interfaces.

在这篇文章中,我们将从继承的必要性开始,转到继承如何与类和接口一起工作。

Then, we’ll cover how the variable/ method names and access modifiers affect the members that are inherited.

然后,我们将介绍变量/方法名和访问修饰符如何影响被继承的成员。

And at the end, we’ll see what it means to inherit a type.

而在最后,我们将看到继承一个类型意味着什么。

2. The Need for Inheritance

2.继承的必要性

Imagine, as a car manufacturer, you offer multiple car models to your customers. Even though different car models might offer different features like a sunroof or bulletproof windows, they would all include common components and features, like engine and wheels.

想象一下,作为一个汽车制造商,你向你的客户提供多种汽车型号。即使不同的汽车型号可能提供不同的功能,如天窗或防弹窗,但它们都会包括共同的组件和功能,如发动机和车轮。

It makes sense to create a basic design and extend it to create their specialized versions, rather than designing each car model separately, from scratch.

创建一个基本的设计,并将其扩展到创建他们的专门版本,而不是从头开始单独设计每个汽车模型,这是合理的。

In a similar manner, with inheritance, we can create a class with basic features and behavior and create its specialized versions, by creating classes, that inherit this base class. In the same way, interfaces can extend existing interfaces.

以类似的方式,通过继承,我们可以创建一个具有基本特征和行为的类,并通过创建继承这个基类的类来创建其专门版本。以同样的方式,接口可以扩展现有的接口。

We’ll notice the use of multiple terms to refer to a type which is inherited by another type, specifically:

我们会注意到使用多个术语来指代一个被另一个类型所继承的类型,具体而言。

  • a base type is also called a super or a parent type
  • a derived type is referred to as an extended, sub or a child type

3. Class Inheritance

3.类的继承性

3.1. Extending a Class

3.1.扩展一个类

A class can inherit another class and define additional members.

一个类可以继承另一个类并定义额外的成员。

Let’s start by defining a base class Car:

让我们从定义一个基类Car开始。

public class Car {
    int wheels;
    String model;
    void start() {
        // Check essential parts
    }
}

The class ArmoredCar can inherit the members of Car class by using the keyword extends in its declaration:

ArmoredCar可以通过在其声明中使用关键字extends来继承Car类的成员。

public class ArmoredCar extends Car {
    int bulletProofWindows;
    void remoteStartCar() {
	// this vehicle can be started by using a remote control
    }
}

We can now say that the ArmoredCar class is a subclass of Car, and the latter is a superclass of ArmoredCar.

我们现在可以说,ArmoredCar类是Car的子类,而后者是ArmoredCar的超类。

Classes in Java support single inheritance; the ArmoredCar class can’t extend multiple classes.

Java中的类支持单一继承ArmoredCar类不能扩展多个类。

Also, note that in the absence of an extends keyword, a class implicitly inherits class java.lang.Object.

另外,请注意,在没有extends关键字的情况下,一个类隐含地继承了java.lang.Object类。

A subclass class inherits the non-static protected and public members from the superclass class. In addition, the members with default (package-private) access are inherited if the two classes are in the same package.

子类继承了来自超类的非静态的保护的公共的成员。此外,如果两个类在同一个包中,那么具有默认包-私有)访问的成员也被继承。

On the other hand, the private and static members of a class are not inherited.

另一方面,一个类的privatestatic成员是不能继承的。

3.2. Accessing Parent Members from a Child Class

3.2.从子类中访问父类成员

To access inherited properties or methods, we can simply use them directly:

要访问继承的属性或方法,我们可以简单地直接使用它们。

public class ArmoredCar extends Car {
    public String registerModel() {
        return model;
    }
}

Note that we don’t need a reference to the superclass to access its members.

注意,我们不需要对超类的引用来访问其成员。

4. Interface Inheritance

4.接口继承

4.1. Implementing Multiple Interfaces

4.1.实现多个接口

Although classes can inherit only one class, they can implement multiple interfaces.

尽管类只能继承一个类,但它们可以实现多个接口。

Imagine the ArmoredCar that we defined in the preceding section is required for a super spy. So the Car manufacturing company thought of adding flying and floating functionality:

想象一下,我们在上一节中定义的ArmoredCar是一个超级间谍所需要的。所以汽车制造公司想到了增加飞行和漂浮功能。

public interface Floatable {
    void floatOnWater();
}
public interface Flyable {
    void fly();
}
public class ArmoredCar extends Car implements Floatable, Flyable{
    public void floatOnWater() {
        System.out.println("I can float!");
    }
 
    public void fly() {
        System.out.println("I can fly!");
    }
}

In the example above, we notice the use of the keyword implements to inherit from an interface.

在上面的例子中,我们注意到使用了关键词implements来继承一个接口。

4.2. Issues With Multiple Inheritance

4.2.多重继承的问题

Java allows multiple inheritance using interfaces.

Java允许使用接口进行多重继承。

Until Java 7, this wasn’t an issue. Interfaces could only define abstract methods, that is, methods without any implementation. So if a class implemented multiple interfaces with the same method signature, it was not a problem. The implementing class eventually had just one method to implement.

在Java 7之前,这并不是一个问题。接口只能定义抽象方法,也就是说,没有任何实现的方法。因此,如果一个类用相同的方法签名实现了多个接口,这并不是一个问题。实现类最终只有一个方法需要实现。

Let’s see how this simple equation changed with the introduction of default methods in interfaces, with Java 8.

让我们看看这个简单的等式是如何随着Java 8在接口中引入default方法而改变的。

Starting with Java 8, interfaces could choose to define default implementations for its methods (an interface can still define abstract methods). This means that if a class implements multiple interfaces, which define methods with the same signature, the child class would inherit separate implementations. This sounds complex and is not allowed.

从Java 8开始,接口可以选择为其方法定义默认的实现(一个接口仍然可以定义抽象方法)。这意味着,如果一个类实现了多个接口,而这些接口定义了具有相同签名的方法,那么子类将继承不同的实现。这听起来很复杂,是不允许的。

Java disallows inheritance of multiple implementations of the same methods, defined in separate interfaces.

Java不允许在不同的接口中定义同一方法的多个实现的继承。

Here’s an example:

这里有一个例子。

public interface Floatable {
    default void repair() {
    	System.out.println("Repairing Floatable object");	
    }
}
public interface Flyable {
    default void repair() {
    	System.out.println("Repairing Flyable object");	
    }
}
public class ArmoredCar extends Car implements Floatable, Flyable {
    // this won't compile
}

If we do want to implement both interfaces, we’ll have to override the repair() method.

如果我们确实想实现这两个接口,我们就必须重写repair()方法。

If the interfaces in the preceding examples define variables with the same name, say duration, we can’t access them without preceding the variable name with the interface name:

如果前面的例子中的接口定义了同名的变量,比如说duration,如果不在变量名前加上接口名,我们就无法访问它们。

public interface Floatable {
    int duration = 10;
}
public interface Flyable {
    int duration = 20;
}
public class ArmoredCar extends Car implements Floatable, Flyable {
 
    public void aMethod() {
    	System.out.println(duration); // won't compile
    	System.out.println(Floatable.duration); // outputs 10
    	System.out.println(Flyable.duration); // outputs 20
    }
}

4.3. Interfaces Extending Other Interfaces

4.3.扩展其他接口的接口

An interface can extend multiple interfaces. Here’s an example:

一个接口可以扩展多个接口。这里有一个例子。

public interface Floatable {
    void floatOnWater();
}
interface interface Flyable {
    void fly();
}
public interface SpaceTraveller extends Floatable, Flyable {
    void remoteControl();
}

An interface inherits other interfaces by using the keyword extends. Classes use the keyword implements to inherit an interface.

一个接口通过使用关键字extends来继承其他接口。类使用关键字implements来继承一个接口。

5. Inheriting Type

5.继承类型

When a class inherits another class or interfaces, apart from inheriting their members, it also inherits their type. This also applies to an interface that inherits other interfaces.

当一个类继承另一个类或接口时,除了继承它们的成员外,它还继承它们的类型。这也适用于一个继承其他接口的接口。

This is a very powerful concept, which allows developers to program to an interface (base class or interface), rather than programming to their implementations.

这是一个非常强大的概念,它允许开发者按照接口(基类或接口)编程,而不是按照其实现编程。

For example, imagine a condition, where an organization maintains a list of the cars owned by its employees. Of course, all employees might own different car models. So how can we refer to different car instances? Here’s the solution:

例如,想象一下这样的情况,一个组织维护着其员工所拥有的汽车清单。当然,所有员工可能拥有不同的汽车型号。那么,我们怎样才能引用不同的汽车实例呢?这里有一个解决方案。

public class Employee {
    private String name;
    private Car car;
    
    // standard constructor
}

Because all derived classes of Car inherit the type Car, the derived class instances can be referred by using a variable of class Car:

因为Car的所有派生类都继承了Car类型,派生类的实例可以通过使用Car类的变量来引用。

Employee e1 = new Employee("Shreya", new ArmoredCar());
Employee e2 = new Employee("Paul", new SpaceCar());
Employee e3 = new Employee("Pavni", new BMW());

6. Hidden Class Members

6.隐藏的类成员

6.1. Hidden Instance Members

6.1.隐藏的实例成员

What happens if both the superclass and subclass define a variable or method with the same name? Don’t worry; we can still access both of them. However, we must make our intent clear to Java, by prefixing the variable or method with the keywords this or super.

如果超类和子类都定义了同名的变量或方法会怎样?别担心,我们仍然可以同时访问它们。但是,我们必须向Java表明我们的意图,在变量或方法的前面加上关键字thissuper

The this keyword refers to the instance in which it’s used. The super keyword (as it seems obvious) refers to the parent class instance:

this关键字指的是它所使用的实例。super关键字(似乎很明显)指的是父类实例。

public class ArmoredCar extends Car {
    private String model;
    public String getAValue() {
    	return super.model;   // returns value of model defined in base class Car
    	// return this.model;   // will return value of model defined in ArmoredCar
    	// return model;   // will return value of model defined in ArmoredCar
    }
}

A lot of developers use this and super keywords to explicitly state which variable or method they’re referring to. However, using them with all members can make our code look cluttered.

很多开发者使用thissuper关键字来明确说明他们所指的是哪个变量或方法。然而,对所有成员使用它们会使我们的代码看起来很杂乱。

6.2. Hidden Static Members

6.2.隐藏的静态成员

What happens when our base class and subclasses define static variables and methods with the same name? Can we access a static member from the base class, in the derived class, the way we do for the instance variables?

当我们的基类和子类定义了同名的静态变量和方法时,会发生什么情况呢?我们能否像访问实例变量那样,在派生类中访问基类中的静态成员?

Let’s find out using an example:

让我们通过一个例子来了解一下。

public class Car {
    public static String msg() {
        return "Car";
    }
}
public class ArmoredCar extends Car {
    public static String msg() {
        return super.msg(); // this won't compile.
    }
}

No, we can’t. The static members belong to a class and not to instances. So we can’t use the non-static super keyword in msg().

不,我们不能。静态成员属于一个类,而不是实例。所以我们不能在msg()中使用非静态的super关键字。

Since static members belong to a class, we can modify the preceding call as follows:

由于静态成员属于一个类,我们可以将前面的调用修改如下。

return Car.msg();

Consider the following example, in which both the base class and derived class define a static method msg() with the same signature:

考虑下面的例子,基类和派生类都定义了一个静态方法msg(),其签名相同。

public class Car {
    public static String msg() {
        return "Car";
    }
}
public class ArmoredCar extends Car {
    public static String msg() {
        return "ArmoredCar";
    }
}

Here’s how we can call them:

下面是我们如何称呼他们。

Car first = new ArmoredCar();
ArmoredCar second = new ArmoredCar();

For the preceding code, first.msg() will output “Car and second.msg() will output “ArmoredCar”. The static message that is called depends on the type of the variable used to refer to ArmoredCar instance.

对于前面的代码,first.msg()将输出 “Carsecond.msg()将输出 “ArmoredCar”。被调用的静态消息取决于用于引用ArmoredCar实例的变量的类型。

7. Conclusion

7.结论

In this article, we covered a core aspect of the Java language – inheritance.

在这篇文章中,我们介绍了Java语言的一个核心方面–继承。

We saw how Java supports single inheritance with classes and multiple inheritance with interfaces and discussed the intricacies of how the mechanism works in the language.

我们看到了Java是如何支持类的单继承和接口的多继承的,并讨论了该机制在语言中的错综复杂的运作。

As always, the full source code for the examples is available over on GitHub.

一如既往,这些示例的完整源代码可在GitHub上获得