This constructor passes the name and value up to the corresponding two-argument superclass constructor. It
then invokes its own decodeColor method to make myColor hold a reference to the correct
ScreenColor object.
You can defer the choice of which superclass constructor to use by explicitly invoking one of your class's own
constructors using this instead of super, as shown in the second constructor of ColorAttr.
public ColorAttr(String name) {
this(name, "transparent");
}
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 constructors as your constructor's first
executable statement, the superclass's no-arg constructor is automatically invoked before any statements 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 constructor, 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
parameters to the superclass constructors based on few or no parameters of its own constructor. It is common
to have an extended class that has no constructor signatures in common with its superclass.
Constructors are not methods and are not inherited. If the superclass defines a number of constructors and an
extended class wishes to have constructors of the same form, then the extended class must explicitly declare
each constructor, even if all that constructor does is invoke the superclass constructor of the same form.