Java Reference
In-Depth Information
Recall that the method center was defined in the class Figure and that the
definition of the method center included an invocation of the method draw . If,
contrary to fact, Java used early binding, then when the code for the method center
compiles, the invocation of the method draw would be bound to the currently available
definition of draw , which is the one given in the definition of Figure . If early binding
were used, the method center would behave exactly the same for all derived classes of
the class Figure as it does for objects created using the class Figure . But, fortunately,
Java uses late binding, so when center is invoked by an object of the class Triangle ,
the invocation of draw (inside the method center ) is not bound to a definition of
draw until the invocation actually takes place. At this point in time, the run-time
system knows the calling object is an instance of the class Triangle and so uses the
definition of draw given in the definition of the class Triangle (even if the invocation
of draw is inside the definition of the method center ). So, the method center
behaves differently for an object of the class Triangle than it would for an object that
is just a plain old Figure . With late binding, as in Java, things automatically work out
the way you normally want them to.
Note that in order for late binding to work, each object must somehow know which
definition of each method applies to that object. So, when an object is created in a
system using late binding, the description of the object must include (either directly or
indirectly) a description of where the appropriate definition of each method is located.
This additional overhead is the penalty you pay for the convenience of late binding.
VideoNote
Late Binding
Example
Late Binding
With late binding , the definition of a method is not bound to an invocation of the method
until run time—in fact, not until the time at which the particular invocation takes place. Java
uses late binding (for all methods except those discussed in the Pitfall section entitled “No
Late Binding for Static Methods”).
polymorphism
The terms polymorphism and late binding are essentially just different words for
the same concept. The term polymorphism refers to the processes of assigning multiple
meanings to the same method name using late binding.
Polymorphism
Polymorphism refers to the ability to associate many meanings to one method name
by means of the late binding mechanism. Thus, polymorphism and late binding are really
the same topic.
 
Search WWH ::




Custom Search