Java Reference
In-Depth Information
You can also define default values in the declarations of the attributes for the class. For
example, you could declare the classification variable with:
protected Classification classification = DISPLAY;
Establishing defaults with constructors has the advantage of placing all an object's
initialization in one place—its constructors—rather than partly in its constructors and partly in
its attribute declarations.
SOLUTION 14.4
Here are a pair of Fountain constructors that mirror the constructors in the Firework
superclass:
public Fountain(String name)
{
super(name);
}
public Fountain(
String name, Classification classification)
{
super(name, classification);
}
The first constructor could invoke this() with the name parameter and with a default
classification. The code here has the advantage of leaving the decision of which classification
is the default in one place, the superclass. On the other hand, you may have picked up on the
fact that fountains are always "consumer" fireworks in the United States, and you may have
built that into your solution, which is arguably an improvement on the code shown here.
Search WWH ::




Custom Search