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

 

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值