Chapter 9 Object-Oriented Programming 451 Shape TwoDimensionalShape ThreeDimensionalShape (How to cite a web site)

Chapter 9 Object-Oriented Programming 451 Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Tetrahedron Fig. 9.3 A portion of a Shapeclass hierarchy. public class TwoDimensionalShape extends Shape { … } With inheritance, privatemembers of a superclass are not directly accessible from that class s subclasses. Package access members of the superclass are accessible in a subclass only if both the superclass and its subclass are in the same package. All other superclass members become members of the subclass, using their original member access (i.e., public members of the superclass become public members of the subclass, and protectedmembers of the superclass become protectedmembers of the subclass). Software Engineering Observation 9.3 Constructors are never inherited they are specific to the class in which they are defined. It is possible to treat superclass objects and subclass objects similarly; that commonality is expressed in the attributes and behaviors of the superclass. Objects of all classes derived from a common superclass can all be treated as objects of that superclass. We will consider many examples in which we can take advantage of this inheritance relationship with an ease of programming not available in non-object-oriented languages such as C. 9.3 protectedMembers A superclass s public members are accessible anywhere the program has a reference to that superclass type or one of its subclass types. A superclass s private members are accessible only in methods of that superclass. A superclass s protected access members serve as an intermediate level of protection between public and private access. A superclass s protected members may be accessed only by methods of the superclass, by methods of subclasses and by methods of other classes in the same package (protected members have package access). Subclass methods can normally refer to publicand protectedmembers of the superclass simply by using the member names. When a subclass method overrides a superclass method, the superclass method may be accessed from the subclass by preceding the superclass method name with keyword super followed by the dot operator (.). This technique is illustrated several times throughout the chapter. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply