Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 7
Second, why implement StartStop in Vehicle when its methods are empty? Couldn't you just implement StartStop in Car? You could, but suppose all types of vehicles express some commonality during startup. Rather than repeat that commonality
in each subclass, express it once in the superclass. Also, by factoring StartStop's methods into Vehicle, you take advantage of polymorphism -- a concept I will discuss next month.
Finally, what does StartStop ss = c; mean? ss is a reference variable that has the StartStop type. That variable can hold a reference to an object whose class implements the StartStop interface. Because c contains a reference to a Car object, and because Car implements StartStop, you can legally assign c's reference to variable ss. That capability has implications though. For example, when a Car object reference assigns to a Car variable, you have full access to that object's nonprivate members. However, when a Car object reference assigns to a StartStop variable, your access narrows to only those members that StartStop declares as well as all Object methods inherited and/or overridden in the class. For example, using ss, it is legal to call start() and toString(), but not getName().
Java allows classes to implement multiple interfaces -- a capability known as multiple interface inheritance. Suppose you want to write a program that counts the number of times you have failed to start a washing machine. You would like to use that counting capability together with the previous starting and stopping (with diagnosis) capability.
One way to introduce the counting capability is to specify it inside an appropriate class that represents your washing machine.
However, because counting is a generic concept, you probably do not want to tie it to a specific implementation. Instead,
you could introduce the counting capability as an interface that extends DiagnoseStartStop. But doing so indicates that counting is a kind of diagnostic starting and stopping capability, which has implications. For
example, if you include counting with starting and stopping, you say that you can also start and stop geese in addition to
counting them as they fly overhead -- I assume you do not have a gun. Also, you say that you can start and stop goals scored
at a hockey game (in addition to tracking the goals your team scored). To separately deal with counting, you must introduce
an independent interface that represents that capability, as shown in Listing 4's Counter interface:
Listing 4. Counter.java
// Counter.java
interface Counter
{
void increment ();
int getCount ();
}
The Counter interface introduces a counter type into source code. When a class implements Counter, its objects can be thought of as counters: somewhere inside a counter object is a field that keeps track of a count. The
increment operation advances the counter by some increment (which is usually one, but could be some other value, such as six
-- to account for a touchdown in a football simulation), and the get operation retrieves the current counter value.