Java Reference
In-Depth Information
8
Don't make any commitments until you have to.
ANONYMOUS
Introduction
The three main programming mechanisms that constitute object-oriented programming
(OOP) are encapsulation, inheritance, and polymorphism. We have already covered the
first two. In this chapter, we discuss polymorphism. Polymorphism refers to the ability
to associate many meanings to one method name by means of a special mechanism
known as late binding or dynamic binding .
This chapter also covers abstract classes , which are classes in which some methods
are not fully defined. Abstract classes are designed to be used only as base classes for
defining new classes. You cannot create instances of (objects of) an abstract class; you
can only create instances of its descendent classes.
Both polymorphism and abstract classes deal with code in which a method is used
before it is defined. Although this may sound paradoxical, it all works out smoothly
in Java.
Prerequisites
This chapter requires Chapters 1 through 5 and Chapter 7 , with the exception that
Section 5.4 on packages and javadoc is not required. This chapter does not use any
material on arrays from Chapter 6 .
Sections 8.1 on polymorphism and 8.2 on abstract classes are independent of each
other, and you may cover Section 8.2 before Section 8.1 if you wish.
8.1 Polymorphism
I did it my way.
FRANK SINATRA
Inheritance allows you to define a base class and to define software for the base class.
That software can then be used not only for objects of the base class but also for objects
of any class derived from the base class. Polymorphism allows you to make changes
in the method definition for the derived classes and to have those changes apply to
the software written in the base class . This all happens automatically in Java, but it
is important to understand the process. To understand polymorphism, we need a
concrete example. The next subsection begins with such an example.
 
Search WWH ::




Custom Search