the future without affecting clients of the class.
Every class you have seen so far is an extended class, whether or not it is declared as such. A class such as
Attr that does not explicitly extend another class implicitly extends the Object class. Object is at the
root of the class hierarchy. The Object class declares methods that are implemented by all objectssuch as
the toString method you saw in Chapter 2. Variables of type Object can refer to any object, whether it is
a class instance or an array. The Object class itself is described in more detail on page 99.
The next class extends the notion of attribute to store color attributes, which might be strings that name or
describe colors. Color descriptions might be color names like "red" or "ecru" that must be looked up in a table,
or numeric values that can be decoded to produce a standard, more efficient color representation we call
ScreenColor (assumed to be defined elsewhere). Decoding a description into a ScreenColor object is
expensive enough that you would like to do it only once. So we extend the Attr class to create a
ColorAttr class to support a method to retrieve a decoded ScreenColor object. We implement it so the
decoding is done only once:
class ColorAttr extends Attr {
private ScreenColor myColor; // the decoded color
public ColorAttr(String name, Object value) {
super(name, value);
decodeColor();
}
public ColorAttr(String name) {
this(name, "transparent");
}
public ColorAttr(String name, ScreenColor value) {
super(name, value.toString());
myColor = value;
}
public Object setValue(Object newValue) {
// do the superclass's setValue work first
Object retval = super.setValue(newValue);
decodeColor();
return retval;
}
/* Set value to ScreenColor, not description /
public ScreenColor setValue(ScreenColor newValue) {
// do the superclass's setValue work first
super.setValue(newValue.toString());
ScreenColor oldValue = myColor;
myColor = newValue;
return oldValue;
}
/* Return decoded ScreenColor object /
public ScreenColor getColor() {
return myColor;
}
/* set ScreenColor from description in getValue /
protected void decodeColor() {
if (getValue() == null)
myColor = null;
else
myColor = new ScreenColor(getValue());
}
}