您的位置:首页 > 编程语言

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: