Section 8.8 Chapter Summary
Subsection 8.8.1 Principles of Object-Oriented Design
To conclude this chapter, it will be helpful to focus briefly on how the examples we've seen address the various object-oriented design (OOD) principles we set out at the beginning of the book.
Divide-and-Conquer Principle: Notice how all of the problems tackled in this chapter have been solved by dividing them into several classes, with each of the classes divided into separate methods. The very idea of a class hierarchy is an application of this principle.
Encapsulation Principle: The superclasses in our designs,
TwoPlayerGame, encapsulate those features of the class hierarchy that are shared by all objects in the hierarchy. The subclasses,
OneRowNim, encapsulate features that make them distinctive with the class hierarchy. Interface Principle. The several Java interfaces we've designed,
UserInterface, specify clearly how various types of related objects will interact with each other through the methods contained in the interfaces. Clean interfaces make for clear communication among objects.
Information Hiding Principle: We have continued to make consistent use of the
publicqualifiers, and have now introduced the
protectedqualifier to extend this concept. The inheritance mechanism gives subclasses access to protected and public elements of their superclasses.
Generality Principle: As you move down a well-designed class hierarchy, you go from the more general to the more specific features of the objects involved. The abstract encode() method specifies the general form that encoding will take while the various implementations of this method in the subclasses provide the specializations necessary to distinguish, say, Caesar encoding from Transpose encoding. Similarly, the abstract
makeAMove()method in the
IPlayerinterface provides a general format for a move in a two-player game, while its various implementations provide the specializations that distinguish one game from another.
Extensibility Principle:Overriding inherited methods and implementing abstract methods from either an abstract superclass or a Java interface provide several well-designed ways to extend the functionality in an existing class hierarchy. Extending a class is a form of specialization of the features inherited from the superclass.
Abstraction Principle: Designing a class hierarchy is an exercise in abstraction, as the more general features of the objects involved are moved into the superclasses. Similarly, designing a Java interface or an abstract superclass method is a form of abstraction, whereby the signature of the method is distinguished from its various implementations.
These, then, are some of the ways that the several examples we have considered and this chapter's discussion have contributed to a deepening of our understanding of object-oriented design.
Subsection 8.8.2 Technical Terms
|actual type (dynamic type)||polymorphic method|
|class inheritance||static binding (early binding)|
|cryptography||static type (declared type)|
|dynamic binding (late binding)||substitution cipher|
Subsection 8.8.3 Summary of Important Points
Inheritance is an object-oriented mechanism whereby subclasses inherit the public and protected instance variables and methods from their superclasses.
Dynamic binding (or late binding) is the mechanism by which a method call is bound to (associated with) the correct implementation of the method at run time. In Java, all method calls, except for
privatemethods, are resolved using dynamic binding.
Static binding (or early binding) is the association of a method call with its corresponding implementation at compile time.
Polymorphism is an object-oriented language feature in which a method call can lead to different actions depending on the object on which it is invoked. A polymorphic method is a method signature that is given different implementation by different classes in a class hierarchy.
A static type is a variable's declared type. A dynamic type, or actual type, is the type of object assigned to that variable at a given point in a running program.
abstractmethod is a method definition that lacks an implementation. An
abstractclass is one that contains one or more
abstractclass can be subclassed but not instantiated.
A Java interface is a class that contains only method signatures and (possibly) constant declarations, but no variables. An interface can be implemented by a class by providing implementations for all of its abstract methods.