Java Reference
In-Depth Information
Early binding of methods occurs if the compiler selects a method from several
possible candidates. Late binding occurs if the method selection takes place when
the program runs.
S ELF C HECK
5. Why is it impossible to construct a Measurable object?
6. Why can you nevertheless declare a variable whose type is
Measurable ?
7. What do overloading and polymorphism have in common? Where do
they differ?
9.4 Using Interfaces for Callbacks
In this section, we discuss how the DataSet class can be made even more reusable
by supplying a different interface type. This type of interface provides a Ȓcallbackȓ
mechanism, allowing the DataSet class to call back a specific method when it
needs more information.
To understand why a further improvement to the DataSet class is desirable,
consider these limitations of the Measurable interface:
ȗ
You can add the Measurable interface only to classes under your control. If
you want to process a set of Rectangle objects, you cannot make the
Rectangle class implement another interfaceȌit is a system class, which
you cannot change.
ȗ
You can measure an object in only one way. If you want to analyze a set of
savings accounts both by bank balance and by interest rate, you are stuck.
Therefore, let us rethink the DataSet class. The data set needs to measure the
objects that are added. When the objects are required to be of type Measurable , the
responsibility of measuring lies with the added objects themselves, which is the cause
of the limitations that we noted. It would be better if another object could carry out
the measurement. Let's move the measurement method into a different interface:
Search WWH ::




Custom Search