JHTP小结_第九章_面向对象编程-继承(Inheritance)
2016-07-15 23:28
260 查看
Section9.1 Introduction
• Inheritance (p. 361)reduces program-development time.
• The direct superclass(p. 361) of a subclass is the one from which the subclass inherits. An indirect superclass (p. 361) of a subclass is two or more levels up the
class hierarchy from that subclass.
• In single inheritance(p. 361), a class is derived from one superclass. In multiple inheritance, a classis 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 (p.361).
• 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.
• Anis-arelationship
(p. 362) represents inheritance. In anis-arelationship,
an object of a subclass also can be treated as an object of its superclass.
• Ahas-arelationship
(p. 362) represents composition. In ahas-arelationship,
a class object contains references to objects of other classes.
Section9.2 Superclasses and Subclasses
• Single-inheritance relationships form treelike hierarchical structures—a superclass exists in a hierarchical relationship with its subclasses.
Section9.3protectedMembers
• A superclass’spublicmembers
are accessible wherever the program has a reference to an objectof that superclass or one of its subclasses.
• A superclass’sprivatemembers
can be accessed directly only within the superclass’s declaration.
• A superclass’sprotectedmembers
(p. 364) have an intermediate level of protection betweenpublicandprivateaccess.
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’sprivatemembers
are hidden in its subclasses and can be accessed only through thepublicorprotectedmethods
inherited from the superclass.
• An overridden super classmethod can be accessed from a subclass if the superclass method name
is preceded bysuper(p.
364) and a dot (.) separator.
Section9.4 Relationship Between Superclasses and Subclasses
• A subclass cannot access theprivatemembers
of its superclass, but it can access the non-private
members.
• A subclass can invoke a constructor of its superclass by using the keywordsuper,
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@Overrideannotation
(p. 369) 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 super type.”
• MethodtoStringtakes
no arguments and returns aString. TheObjectclass’stoStringmethod
is normally overridden by a subclass.
• When an object is output using the%sformat
specifier, the object’stoStringmethod
is called
implicitly to obtain itsStringrepresentation.
Section9.5 Constructors in Subclasses
• The first task of a subclass constructor is to call its direct superclass’s constructor (p. 378) to ensure that the instance variables inherited from the superclass
are initialized.
Section9.6 ClassObject
• See the table of classObject’s
methods in Fig. 9.12.
相关文章推荐
- laravel中的自定义函数的放置规范
- Struts2内置校验器——完整实例代码
- Python序列——Unicode
- 向图片任意位置添加logo
- Thinking in C++: 第1章 对象=特性+行为
- myeclipse2014安装aptana
- spring boot 简单例子
- 深入理解ThreadPoolExecutor
- Thinking in C++: 第1章 对象的演化
- c++知识点大总结
- java-基础-变量
- BCD码详解
- C 语言数组初始化
- 优雅编程之这样处理边界,你就“正常”了!
- GetPrivateProfileString
- 用python进行自然语言处理
- 深入理解Java虚拟机--类加载器
- QT5的exe的发布
- python之描述符
- java第三方类库实现图片等比缩放