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,
Cipher
andTwoPlayerGame
, encapsulate those features of the class hierarchy that are shared by all objects in the hierarchy. The subclasses,CaesarCipher
andOneRowNim
, encapsulate features that make them distinctive with the class hierarchy. Interface Principle. The several Java interfaces we’ve designed,IPlayer
,CLUIPlayableGame
andUserInterface
, 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
private
andpublic
qualifiers, and have now introduced theprotected
qualifier 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 theIPlayer
interface 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
abstract method | plaintext |
actual type (dynamic type) | polymorphic method |
ciphertext | polymorphism |
class inheritance | static binding (early binding) |
cryptography | static type (declared type) |
dynamic binding (late binding) | substitution cipher |
interface | transposition cipher |
overloaded method |
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
final
orprivate
methods, 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.
- An
abstract
method is a method definition that lacks an implementation. Anabstract
class is one that contains one or moreabstract
methods. Anabstract
class 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.
Solutions 8.8.4 Solutions to Self-Study Exercises
8.2 Java’s Inheritance Mechanism
8.2.4 Polymorphism and Object-Oriented Design
Self-Study Exercises
8.2.5 Using super
to Refer to the Superclass
Self-Study Exercises
8.2.5.1. B subclasses A.
8.2.5.2. B subclasses A, Part 2.
8.2.5.3. Which are valid?
8.2.6 Inheritance and Constructors
Self-Study Exercises
8.2.6.1. Super Constructors.
8.3 Abstract Classes, Interfaces, and Polymorphism
8.3.1 Implementing an Abstract Method
Self-Study Exercises
8.3.1.1. Pig subclass.
8.3.1.2. Pig talk.
8.3.2 Implementing a Java Interface
Exercises
8.3.2.1. Speakable interface.
8.4 Example: A Toggle Button
8.4.3 Swapping Algorithm
Self-Study Exercises
8.4.3.1. Swap Error.
8.6 Example: The Cipher Class Hierarchy
8.6.5 Testing and Debugging
Self-Study Exercises
8.6.5.1. Caesar Shift.
8.6.5.2. Transpose Rotate.
8.7 Case Study: A Two Player Game Hierarchy
8.7.11 Playing OneRowNim
Self-Study Exercises
8.7.11.1. Improved One Row Nim.
You have attempted of activities on this page.