Section 8.1 Introduction
Among the most important concepts in object oriented programming are the concepts of inheritance and polymorphism. We first introduced the idea of inheritance in Chapter 0. There we compared inheritance to the natural form of inheritance, in which horses and cows share certain inherited characteristics, such as being warm-blooded, by virtue of their being mammals.
We took a more technical look at inheritance in Chapter 3, where we talked about the
toString()
method and how it is inherited from the Object
class. We illustrated there how subclasses of Object
could override the inherited toString()
method in order to customize it for their purposes. We also introduced the idea of polymorphism, in which a method call, such as obj.toString()
, can have different behaviors depending on the type of object, obj
, on which it is called.In Chapter 4, we continued introducing inheritance and polymorphism, when we learned about Java’s Abstract Windowing Toolkit (AWT) and Swing hierarchies, the class hierarchies that are used to create Graphical User Interfaces (GUIs). We also learned how to extend a class to create our own subclass, and we made limited use of inheritance in the design of the
SimpleGUI
class. We were also introduced to the concept of a Java interface, and we learned how to use the ActionListener
interface to enable a SimpleGUI
to handle action events while the GUI is running.In this chapter we will take a much closer look at these important object-oriented concepts. We will learn how Java’s dynamic binding mechanism works and how it makes polymorphism possible. Most importantly, we will see why inheritance and polymorphism are important elements of object-oriented design, and we will learn how to use these important tools to design several different programs. In keeping with our running games example, we will develop a
TwoPlayerGame
hierarchy and show how it can simplify the implementation of OneRowNim
and other two-player games.You have attempted of activities on this page.