Java Reference
In-Depth Information
Late Binding
Suppose you are designing software for a graphics package that has classes for several kinds
of figures, such as rectangles, circles, ovals, and so forth. Each figure might be an object
of a different class. For example, the
Rectangle
class might have instance variables for a
height, width, and center point, while the
Circle
class might have instance variables for
a center point and a radius. In a well-designed programming project, all of these classes
would be descendents of a single parent class called, for example,
Figure
. Now, suppose
you want a method to draw a figure on the screen. To draw a circle, you need different
instructions from those you need to draw a rectangle. So, each class needs to have a
different method to draw its kind of figure. However, because the methods belong to the
classes, they can all be called
draw
. If
r
is a
Rectangle
object and
c
is a
Circle
object,
then
r.draw()
and
c.draw()
can be methods implemented with different code. All
this is not new, but next we are going to expand on this.
Now, the parent class
Figure
may have methods that apply to all figures. For
example, it might have a method called
center
that moves a figure to the center
of the screen by erasing it and then redrawing it in the center of the screen. The
method
center
of the class
Figure
might use the method
draw
to redraw the figure
in the center of the screen. When you think of using the inherited method
center
with figures of the classes
Rectangle
and
Circle
, you begin to see that there are
complications here.
To make the point clear and more dramatic, let's suppose the class
Figure
is
already written and in use and at some later time you add a class for a brand-new
kind of figure—say, the class
Triangle
. Now
Triangle
can be a derived class of the
class
Figure
, so the method
center
will be inherited from the class
Figure
and thus
should apply to (and perform correctly for!) all
Triangle
s. But there is a complication.
The method
center
uses
draw
, and the method
draw
is different for each type of
figure. But, the method
center
is defined in the class
Figure
, which means the
method
center
was compiled before we wrote the code for the method
draw
of the
class
Triangle
. When we invoke the method
center
with an object of the class
Triangle
, we want the code for the method
center
to use a method that was not
even defined when we compiled the method
center
—namely, the method
draw
for
the class
Triangle
. Can this be made to happen in Java? Yes, it can, and moreover, it
happens automatically. You need not do anything special when you define either the
base class
Figure
or the derived class
Triangle
.
The situation we discussed for the method
center
in the derived class
Triangle
works out as we want because Java uses a mechanism known as
late binding
or
dynamic binding
. Let's see how late binding works in this case involving figure classes.
Binding
refers to the process of associating a method definition with a method
invocation. If the method definition is associated with the method invocation when the
code is compiled, that is called
early binding
. If the method invocation is associated
with the method invocation when the method is invoked (at run time), that is called
late binding
or
dynamic binding
. Java uses late binding for all methods except for a
few cases discussed later in this chapter. Let's see how late binding works in the case of
our method
center
.
late binding
dynamic
binding
binding
early binding