Java Reference
In-Depth Information
9.8 Wrap-Up
This chapter introduced inheritance—the ability to create classes by acquiring an existing
class's members (without copying and pasting the code) and having the ability to embellish
them with new capabilities. You learned the notions of superclasses and subclasses and
used keyword extends to create a subclass that inherits members from a superclass. We
showed how to use the @Override annotation to prevent unintended overloading by indi-
cating that a method overrides a superclass method. We introduced the access modifier
protected ; subclass methods can directly access protected superclass members. You
learned how to use super to access overridden superclass members. You also saw how con-
structors are used in inheritance hierarchies. Finally, you learned about the methods of
class Object , the direct or indirect superclass of all Java classes.
In Chapter 10, Object-Oriented Programming: Polymorphism and Interfaces, we
build on our discussion of inheritance by introducing polymorphism —an object-oriented
concept that enables us to write programs that conveniently handle, in a more general and
convenient manner, objects of a wide variety of classes related by inheritance. After
studying Chapter 10, you'll be familiar with classes, objects, encapsulation, inheritance
and polymorphism—the key technologies of object-oriented programming.
Summary
Section 9.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
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 (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.
•An is-a relationship (p. 362) 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 (p. 362) represents composition. In a has-a relationship, a class object con-
tains references to objects of other classes.
Section 9.2 Superclasses and Subclasses
• Single-inheritance relationships form treelike hierarchical structures—a superclass exists in a hi-
erarchical 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 (p. 364) 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.
 
 
Search WWH ::




Custom Search