Java How to Program学习笔记_章节小结——第九章_面向对象编程之继承(Object-Oriented Programming: Inheritance)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hpdlzu80100/article/details/85771053

重要概念已用粗体标识。

Summary

Section 9.1 Introduction

• Inheritance reduces program-development time.

• The direct superclass of a subclass is the one from which the subclass inherits. An indirect superclass of a subclass is two or more levels up the class hierarchy from that subclass.

• In single inheritance, a class is derived from one superclass. In multiple inheritance, a class is derived from more than one direct superclass. Java does not support multiple inheritance.

• A subclass is more specific than its superclass and represents a smaller group of objects .

• Every object of a subclass is also an object of that class’s superclass. However, a superclass object is not an object of its class’s subclasses.

• An is-a relationship represents inheritance. In an is-a relationship, an object of a subclass also can be treated as an object of its superclass.

• A has-a relationship represents composition. In a has-a relationship, a class object contains references to objects of other classes.

Section 9.2 Superclasses and Subclasses

Single-inheritance relationships form treelike hierarchical structures—a superclass exists in a hierarchical relationship with its subclasses.

Section 9.3 protected Members

• A superclass’s public members are accessible wherever the program has a reference to an object of that superclass or one of its subclasses.

• A superclass’s private members can be accessed directly only within the superclass’s declaration.

• A superclass’s protected members have an intermediate level of protection between public and private access. They can be accessed by members of the superclass, by members of its subclasses and by members of other classes in the same package.

• A superclass’s private members are hidden in its subclasses and can be accessed only through the public or protected methods inherited from the superclass.

• An overridden superclass method can be accessed from a subclass if the superclass method name is preceded by super and a dot (.) separator.

Section 9.4 Relationship Between Superclasses and Subclasses

• A subclass cannot access the private members of its superclass, but it can access the non-private members.

• A subclass can invoke a constructor of its superclass by using the keyword super, followed by a set of parentheses containing the superclass constructor arguments. This must appear as the first statement in the subclass constructor’s body.

• A superclass method can be overridden in a subclass to declare an appropriate implementation for the subclass.

• The @Override annotation indicates that a method should override a superclass method.

When the compiler encounters a method declared with @Override, it compares the method’s signature with the superclass’s method signatures. If there isn’t an exact match, the compiler issues an error message, such as “method does not override or implement a method from a supertype.”

• Method toString takes no arguments and returns a String. The Object class’s toString method is normally overridden by a subclass.

• When an object is output using the %s format specifier, the object’s toString method is called implicitly to obtain its String representation.

Section 9.5 Constructors in Subclasses

• The first task of a subclass constructor is to call its direct superclass’s constructor to ensure that the instance variables inherited from the superclass are initialized.

Section 9.6 Class Object

• See the table of class Object’s methods in Fig. 9.12.

没有更多推荐了,返回首页