ch09 - Inheritance A form of software reuse in which a new...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Inheritance A form of software reuse in which a new class is created by absorbing an existing class’s members and embellishing them with new or modified capabilities. Can save time during program development by basing new classes on existing proven and debugged high-quality software. Increases the likelihood that a system will be implemented and maintained effectively.
Background image of page 2
When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class. Existing class is the superclass New class is the subclass Each subclass can be a superclass of future subclasses. A subclass can add its own fields and methods. A subclass is more specific than its superclass and represents a more specialized group of objects. The subclass exhibits the behaviors of its superclass and can add behaviors that are specific to the subclass. This is why inheritance is sometimes referred to as specialization .
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The direct superclass is the superclass from which the subclass explicitly inherits. An indirect superclass is any class above the direct superclass in the class hierarchy . The Java class hierarchy begins with class Object (in package java.lang ) Every class in Java directly or indirectly extends (or “inherits from”) Object . Java supports only single inheritance , in which each class is derived from exactly one direct superclass.
Background image of page 4
We distinguish between the is-a relationship and the has-a relationship Is-a represents inheritance In an is-a relationship, an object of a subclass can also be treated as an object of its superclass Has-a represents composition In a has-a relationship, an object contains as members references to other objects
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Figure 9.1 lists several simple examples of superclasses and subclasses Superclasses tend to be “more general” and subclasses “more specific.” Because every subclass object is an object of its superclass, and one superclass can have many subclasses, the set of objects represented by a superclass is typically larger than the set of objects represented by any of its subclasses.
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A superclass exists in a hierarchical relationship with its subclasses. Fig. 9.2 shows a sample university community class hierarchy Also called an inheritance hierarchy . Each arrow in the hierarchy represents an is-a relationship. Follow the arrows upward in the class hierarchy an Employee is a CommunityMember “a Teacher is a Faculty member.” CommunityMember is the direct superclass of Employee , Student and Alumnus and is an indirect superclass of all the other classes in the diagram. Starting from the bottom, you can follow the arrows and apply
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 71

ch09 - Inheritance A form of software reuse in which a new...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online