Java Reference
In-Depth Information
10.12 Wrap-Up
This chapter introduced polymorphism—the ability to process objects that share the same
superclass in a class hierarchy as if they were all objects of the superclass. We discussed how
polymorphism makes systems extensible and maintainable, then demonstrated how to use
overridden methods to effect polymorphic behavior. We introduced abstract classes,
which allow you to provide an appropriate superclass from which other classes can inherit.
You learned that an abstract class can declare abstract methods that each subclass must im-
plement to become a concrete class and that a program can use variables of an abstract class
to invoke the subclasses' implementations of abstract methods polymorphically. You also
learned how to determine an object's type at execution time. We explained the notions of
final methods and classes. Finally, the chapter discussed declaring and implementing an
interface as a way for possibly disparate classes to implement common functionality, en-
abling objects of those classes to be processed polymorphically.
You should now be familiar with classes, objects, encapsulation, inheritance, inter-
faces and polymorphism—the most essential aspects of object-oriented programming.
In the next chapter, you'll learn about exceptions, useful for handling errors during a
program's execution. Exception handling provides for more robust programs.
Summary
Section 10.1 Introduction
• Polymorphism (p. 396) enables us to write programs that process objects that share the same su-
perclass as if they were all objects of the superclass; this can simplify programming.
• With polymorphism, we can design and implement systems that are easily extensible. The only
parts of a program that must be altered to accommodate new classes are those that require direct
knowledge of the new classes that you add to the hierarchy.
Section 10.3 Demonstrating Polymorphic Behavior
• When the compiler encounters a method call made through a variable, it determines if the meth-
od can be called by checking the variable's class type. If that class contains the proper method
declaration (or inherits one), the call is compiled. At execution time, the type of the object to
which the variable refers determines the actual method to use.
Section 10.4 Abstract Classes and Methods
• Abstract classes (p. 401) cannot be used to instantiate objects, because they're incomplete.
• The primary purpose of an abstract class is to provide an appropriate superclass from which other
classes can inherit and thus share a common design.
• Classes that can be used to instantiate objects are called concrete classes (p. 402). They provide
implementations of every method they declare (some of the implementations can be inherited).
• Programmers often write client code that uses only abstract superclasses (p. 402) to reduce client
code's dependencies on specific subclass types.
• Abstract classes sometimes constitute several levels of a hierarchy.
• An abstract class normally contains one or more abstract methods (p. 402).
• Abstract methods do not provide implementations.
• A class that contains any abstract methods must be declared as an abstract class (p. 402). Each
concrete subclass must provide implementations of each of the superclass's abstract methods.
 
Search WWH ::




Custom Search