Java Reference
In-Depth Information
We chose to ensure that every color attribute has a color. If a color
value is not supplied we provide a default of "TRansparent" , hence the
one-argument constructor invokes the two-argument constructor using
a default argument.
If you do not invoke a superclass constructor or one of your own con-
structors as your constructor's first executable statement, the super-
class's no-arg constructor is automatically invoked before any state-
ments of the new constructor are executed. That is, your constructor is
treated as if
super();
were its first statement. If the superclass doesn't have a no-arg con-
structor, you must explicitly invoke another constructor.
The third constructor of ColorAttr enables the programmer creating a
new ColorAttr object to specify the ScreenColor object itself.
public ColorAttr(String name, ScreenColor value) {
super(name, value.toString());
myColor = value;
}
The first two constructors must convert their parameters to ScreenColor
objects using the decodeColor method, and that presumably has some
overhead. When the programmer already has a ScreenColor object to
provide as a value, you want to avoid the overhead of that conversion.
This is an example of providing a constructor that adds efficiency, not
capability.
In this example, ColorAttr has constructors with the same signatures as
its superclass's constructors. This arrangement is by no means required.
Sometimes part of an extended class's benefit is to provide useful para-
meters to the superclass constructors based on few or no parameters of
 
Search WWH ::




Custom Search