528 Object-Oriented Programming (Bulletproof web design) Chapter 9 SUMMARY One

528 Object-Oriented Programming Chapter 9 SUMMARY One of the keys to the power of object-oriented programming is achieving software reusability through inheritance. Through inheritance, a new class inherits the instance variables and methods of a previously defined superclass. In this case, the new class is referred to as a subclass. With single inheritance, a class is derived from one superclass. With multiple inheritance, a subclass inherits from multiple superclasses. Java does not support multiple inheritance, but Java does provide the notion of interfaces, which offer many of the benefits of multiple inheritance. A subclass normally adds instance variables and methods of its own, so a subclass generally is larger than its superclass. A subclass represents a smaller set of more specific objects than its superclass. A subclass cannot access the private members of its superclass. A subclass can, however, access the public, protected and package access members of its superclass. The subclass must be in the superclass s package to use superclass members with package access. A subclass constructor always calls the constructor for its superclass first (either explicitly or implicitly) to create and initialize the subclass members inherited from the superclass. Inheritance enables software reusability, which saves time in development and encourages the use of previously proven and debugged high-quality software. An object of a subclass can be treated as an object of its corresponding superclass, but the reverse is not true. A superclass exists in a hierarchical relationship with its subclasses. When a class is used with the mechanism of inheritance, it becomes either a superclass that supplies attributes and behaviors to other classes or a subclass that inherits those attributes and behaviors. An inheritance hierarchy can be arbitrarily deep within the physical limitations of a particular system, but most inheritance hierarchies have only a few levels. Hierarchies are useful for understanding and managing complexity. With software becoming increasingly complex, Java provides mechanisms for supporting hierarchical structures through inheritance and polymorphism. Modifier protected serves as an intermediate level of protection between public access and private access. Superclass protected members may be accessed by methods of the superclass, by methods of subclasses and by methods of classes in the same package. A superclass may be either a direct superclass or an indirect superclass. A direct superclass is the class that a subclass explicitly extends. An indirect superclass is inherited from several levels up the class hierarchy tree. When a superclass member is inappropriate for a subclass, the programmer must override that member in the subclass. In a has a relationship, a class object has a reference to an object of another class as a member. In an is a relationship, an object of a subclass type may also be treated as an object of the superclass type. Is a is inheritance. Has a is composition. A reference to a subclass object may be converted implicitly to a reference for a superclass object. It is possible to convert a superclass reference to a subclass reference by using an explicit cast. If the target object is not a subclass object, a ClassCastException is thrown. A superclass specifies commonality. All classes derived from a superclass inherit the capabilities of that superclass. In the object-oriented design process, the designer looks for commonality among classes and factors it out to form superclasses. Then, subclasses are customized beyond the capabilities inherited from the superclass. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply