An extended class cannot use any new state in the
implementation of the
Let us say you have a
Point class that contains an x and a
y coordinate as integers. That class overrides
You then decide to derive a
PrettyPoint extends Point that
adds a color. Should the
PrettyPoint override the
equals to consider the new color?
PrettyPoint may be used anywhere that a
expected. A plain
Point may be compared to an extended
Because of commutativity, we must guarantee the same result is
prettyPoint.equals(plainPoint). The first method is not
overridden and does not know about color. So a
clearly must ignore color when compared to a plain
PrettyPoint use the color when compared to another
PrettyPoint? Transitivity requires that if
point2 is a plain
then it does not matter if
must ignore the color to avoid breaking transitivity.
There are a couple ways out of this dilemma.
We could make
PrettyPoint and make the
Point colorless. The resulting hierarchies are
unattractive and feel upside down. Each time we add a class
with additional state, we must change all derived classes.
This approach also does not work for a client attempting to
extend third party code.
Another approach is to make a
PrettyPoint contain a
Point with a method for getting that contained
when necessary. This is best when all state is immutable.
Point is an interface rather than a concrete class, then
each implementation of
Point can insist that only objects
from the same implementation be equal. Commutativity and
associativity are preserved. Utility classes can define
specific types of equality through the public methods of
The situation is the same for objects implementing
Arrays do not guarantee runtime type-safety. For example, the
following code compiles without warnings, even with
String strings = new String; Object stuff = strings; stuff = new Integer(13);
There are no explicit casts, yet the code generates a
This is the best reason for Java compiler to prohibit the creation of arrays of generic objects. All guarantees of type safety would be lost.
This is also the reason that Java makes a distinction between a
List and a
List extends Foo>.
If we allowed a
List to be passed as a
List then the compiler would have no way to prevent
Double being added to the latter.
Return to parent directory.