Chapter 9 Object-Oriented Programming 529 Reading a (Web site layout)
Wednesday, October 24th, 2007Chapter 9 Object-Oriented Programming 529 Reading a set of subclass declarations can be confusing, because inherited superclass members are not listed in the subclass declarations, but these members are indeed present in the subclasses. With polymorphism, it becomes possible to design and implement systems that are more extensible. Programs can be written to process objects of types that may not exist when the program is under development. Polymorphic programming can eliminate the need for switch logic, thus avoiding the kinds of errors associated with switchlogic. An abstract method is declared by preceding the method s definition with the keyword abstractin the superclass. There are many situations in which it is useful to define classes for which the programmer never intends to instantiate any objects. Such classes are called abstract classes. Because these are used only as superclasses, typically they are called abstract superclasses. A program cannot instantiate objects of an abstract class. Classes from which a program can instantiate objects are called concrete classes. A class is made abstract by declaring it with the keyword abstract. If a subclass is derived from a superclass with an abstract method without supplying a definition for that abstract method in the subclass, that method remains abstract in the subclass. Consequently, the subclass is also an abstract class. When a request is made through a superclass reference to use a method, Java chooses the correct overridden method in the subclass associated with the object. Through the use of polymorphism, one method call can cause different actions to occur, depending on the type of the object receiving the call. Although we cannot instantiate objects of abstract superclasses, we can declare references to abstract superclasses. Such references can then be used to enable polymorphic manipulations of subclass objects when such objects are instantiated from concrete classes. New classes are regularly added to systems. New classes are accommodated by dynamic method binding (also called late binding). The type of an object need not be known at compile time for a method call to be compiled. At execution time, the appropriate method of the receiving object is selected. With dynamic method binding, at execution time the call to a method is routed to the method version appropriate for the class of the object receiving the call. When a superclass provides a method, subclasses can override the method, but they do not have to override it. Thus a subclass can use a superclass s version of a method. An interface definition begins with the keyword interface and contains a set of publicabstract methods. Interfaces may also contain publicstaticfinal data. To use an interface, a class must specify that it implements the interface and that class must define every method in the interface with the number of arguments and the return type specified in the interface definition. An interface is typically used in place of an abstract class when there is no default implementation to inherit. When a class implements an interface, the same is a relationship provided by inheritance applies. To implement more than one interface, simply provide a comma-separated list of interface names after keyword implements in the class definition. Inner classes are defined inside the scope of other classes. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01