I just noticed that java.util.Observable is a concrete class. Since the purpose of Observable is to be extended, this seems rather odd to me. Is there a reason why it was implemented this way?
I found this article which says that
The observable is a concrete class, so the class deriving from it must be determined upfront, as Java allows only single inheritance.
But that doesn't really explain it to me. In fact, if Observable were abstract, the user would be forced to determine the class deriving from it.
Quite simply it's a mistake that Observable is a class at all, abstract or otherwise.
Observable
should have been an interface and the JDK should have provided a convenient implementation (much likeList
is an interface andArrayList
is an implementation)There are quite a few "mistakes" in java, including:
Arrays.toString(array)
as their defaulttoString()
(how many SO questions has this caused?)Cloneable
shouldn't be a marker interface; it should have theclone()
method andObject.clone()
should not existWhile on the soapbox, in terms of the language itself, IMHO:
==
should execute the.equals()
method (this causes loads of headaches)==
should either be===
like javascript or a dedicated method likeboolean isIdentical(Object o)
, because you hardly ever need it!<
should executecompareTo(Object o) < 0
forComparable
objects (and similarly for>
,<=
,>=
)As a first approach, one could think that this is done to allow the user to use composition instead of inheritance, which is very convenient if your class already inherits from another class, and you cannot inherit from Observable class also.
But if we look to the source code of Observable, we see that there is an internal flag
That is checked everytime the notifyObservers is invoked:
But from a class composed by this Observable, we cannot change this flag, since it is private, and the methods provided to change it are protected.
This means that the user is forced to subclass the Observable class, and I would say that the lack of the "abstract" keyword is just a "mistake".
I would say that this class is a complete screwup.