Java 中的继承 (inheritance)

Inheritance 是 OOP 的重要机制

Java 中的继承是通过在原有类的基础之上扩展新的类来实现的。

Java 中的继承 (inheritance)_第1张图片
java 中的继承解释

上面提到,继承的实现是通过新类的定义来完成的, 我们需要使用关键字 extends,其语法为:

class SubClass extends SuperClass {
     // new fields and method defination
    //  ...
}

子类继承了父类的所有 fields 和 method。并且允许子类对这些继承过来的内容进行更改,这个和我们写 css 的时候是一样的:子集元素直接继承过来的属性也可以进行变更而不影响父级元素的属性。

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html


java 继承总结
Summary of Inheritance

Except for the Object
class, a class has exactly one direct superclass. 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. All classes are descendants from this class and inherit methods from it. Useful methods inherited from Object
include toString(), equals(), clone(), and getClass().
You can prevent a class from being subclassed by using the final
keyword in the class's declaration. 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.

你可能感兴趣的:(Java 中的继承 (inheritance))