Documentation

The Java™ Tutorials
Hide TOC
Summary of Inheritance继承的小结
Trail: Learning the Java Language
Lesson: Interfaces and Inheritance
Section: Inheritance

Summary of Inheritance继承概述

Except for the Object class, a class has exactly one direct superclass. 除了Object类之外,一个类只有一个直接超类。A class inherits fields and methods from all its superclasses, whether direct or indirect. 类从它的所有超类继承字段和方法,无论是直接的还是间接的。A subclass can override methods that it inherits, or it can hide fields or methods that it inherits. 子类可以重写它继承的方法,也可以隐藏它继承的字段或方法。(Note that hiding fields is generally bad programming practice.)(请注意,隐藏字段通常是糟糕的编程实践。)

The table in Overriding and Hiding Methods section shows the effect of declaring a method with the same signature as a method in the superclass.重写和隐藏方法部分中的表显示了使用与超类中的方法相同的签名声明方法的效果。

The Object class is the top of the class hierarchy. Object类是类层次结构的顶部。All classes are descendants from this class and inherit methods from it. 所有类都是该类的后代,并从中继承方法。Useful methods inherited from Object include toString(), equals(), clone(), and getClass().从对象继承的有用方法包括toString()equals()clone()getClass()

You can prevent a class from being subclassed by using the final keyword in the class's declaration. 通过在类的声明中使用final关键字,可以防止类被子类化。Similarly, you can prevent a method from being overridden by subclasses by declaring it as a final method.类似地,通过将方法声明为最终方法,可以防止子类重写该方法。

An abstract class can only be subclassed; it cannot be instantiated. 抽象类只能是子类;它不能被实例化。An abstract class can contain abstract methods—methods that are declared but not implemented. 抽象类可以包含抽象方法—已声明但未实现的方法。Subclasses then provide the implementations for the abstract methods.子类然后提供抽象方法的实现。


Previous page: Abstract Methods and Classes
Next page: Questions and Exercises: Inheritance