Concrete Class in Java – Java中的具体类

最后修改: 2018年 10月 18日

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

1. Introduction

1.介绍

In this quick guide, we’ll discuss the term “concrete class” in Java.

在这份快速指南中,我们将讨论Java中的 “具体类 “一词

First, we’ll define the term. Then, we’ll see how it’s different from interfaces and abstract classes.

首先,我们将定义这个术语。然后,我们将看到它与接口和抽象类有什么不同。

2. What Is a Concrete Class?

2.什么是具体类?

A concrete class is a class that we can create an instance of, using the new keyword.

一个具体的类是一个我们可以创建实例的类,使用new关键字

In other words, it’s a full implementation of its blueprint. A concrete class is complete.

换句话说,它是其蓝图的完整实现。一个具体的类是完整的。

Imagine, for example, a Car class:

想象一下,例如,一个汽车类。

public class Car {
    public String honk() {
        return "beep!";
    }

    public String drive() {
        return "vroom";
    }
}

Because all of its methods are implemented, we call it a concrete class, and we can instantiate it:

因为它的所有方法都被实现了,所以我们称它为一个具体的类,我们可以把它实例化。

Car car = new Car();

Some examples of concrete classes from the JDK are HashMap, HashSet, ArrayList, and LinkedList.

JDK的一些具体类的例子是HashMapHashSetArrayListLinkedList

3. Java Abstraction vs. Concrete Classes

3.Java抽象与具体的类

Not all Java types implement all their methods, though. This flexibility, also called abstraction, allows us to think in more general terms about the domain we’re trying to model.

尽管如此,并不是所有的Java类型都实现了它们的所有方法。这种灵活性,也被称为抽象,使我们能够对我们试图建模的领域进行更广泛的思考。

In Java, we can achieve abstraction using interfaces and abstract classes.

在Java中,我们可以使用接口和抽象类来实现抽象。

Let’s get a better look at concrete classes by comparing them to these others.

让我们通过与这些人的比较来更好地了解具体的类。

3.1. Interfaces

3.1.接口

An interface is a blueprint for a class. Or, in other words, its a collection of unimplemented method signatures:

一个接口是一个类的蓝图。或者,换句话说,它是一个未实现的方法签名的集合。

interface Driveable {
    void honk();
    void drive();
}

Note that it uses the interface keyword instead of class.

注意,它使用了interface关键字而不是class.

Because Driveable has unimplemented methods, we can’t instantiate it with the new keyword.

因为Driveable 有未实现的方法,我们不能用new 关键字将其实例化。

But, concrete classes like Car can implement these methods.

但是,汽车这样的具体类可以实现这些方法。

The JDK provides a number of interfaces like Map, List, and Set.

JDK提供了许多接口,如MapListSet.

3.2. Abstract Classes

3.2.抽象类[/strong]

An abstract class is a class that has unimplemented methods, though it can actually have both:

抽象类是一个拥有未实现方法的类,尽管它实际上可以同时拥有两种方法。

public abstract class Vehicle {
    public abstract String honk();

    public String drive() {
        return "zoom";
    }
}

Note that we mark abstract classes with the keyword abstract.

注意,我们用关键词abstract来标记抽象类。

Again, since Vehicle has an unimplemented method, honk, we won’t be able to use the new keyword.

同样,由于Vehicle 有一个未实现的方法,honk,我们将无法使用new 关键字。

Some examples of abstract classes from the JDK are AbstractMap and AbstractList.

JDK中的一些抽象类的例子是AbstractMapAbstractList

3.3. Concrete Classes

3.3.具体的类

In contrast, concrete classes don’t have any unimplemented methods. Whether the implementations are inherited or not, so long as each method has an implementation, the class is concrete.

相反,具体类没有任何未实现的方法。无论实现是否被继承,只要每个方法都有一个实现,该类就是具体的。

Concrete classes can be as simple as our Car example earlier. They can also implement interfaces and extend abstract classes:

具体类可以像我们前面的Car例子那样简单。它们也可以实现接口并扩展抽象类。

public class FancyCar extends Vehicle implements Driveable {
    public String honk() { 
        return "beep";
    }
}

The FancyCar class provides an implementation for honk and it inherits the implementation of drive from Vehicle.

FancyCar 类为honk提供了一个实现,它从Vehicle.继承了drive的实现。

As such, it has no unimplemented methods. Therefore, we can create a FancyCar class instance with the new keyword.

因此,它没有未实现的方法。因此,我们可以用new关键字创建一个FancyCar类实例。

FancyCar car = new FancyCar();

Or, simply put, all classes which are not abstract, we can call concrete classes.

或者,简单地说,所有不是抽象的类,我们都可以称之为具体的类。

4. Summary

4.总结

In this short tutorial, we learned about concrete classes and their specifications.

在这个简短的教程中,我们了解了具体的类和它们的规格。

Additionally, we showed the differences between interfaces and concrete and abstract classes.

此外,我们还展示了接口与具体和抽象类之间的区别。