Brian Goetz
Posts: 6
Nickname: briangoetz
Registered: Sep, 2005
|
|
Re: Generics and Packages
|
Posted: Jan 23, 2006 1:00 PM
|
|
This does feel like a compiler bug, and I'm not sure what exactly the compiler is confused about, but in any case, there are some awfully questionable things going on in this code example, and the strange behavior goes away when you do something more sensible.
First, the name. ColoredDimension. This is not a "colored dimension", as it is neither a Dimension nor a HasColor. You could implement a ColoredDimension as such:
class ColoredDimension extends Dimension implements HasColor { ... }
with no generics at all.
What ColoredDimension really is is a "colored dimension holder." This may sound like a small nit, but it will be far far less confusing to anyone who wants to read the code (someone already commented on this.)
Second, the use of package-private variables here is extremely confusing. Dimension does not extend HasColor, so clearly you are expecting T to bind to a subclass of Dimension. But Dimension has not been designed for extension! Its fields are package-private, not protected. If you did this, or if Dimension provided getters, the problem goes away. (And clearly it should do one or the other -- the way Dimension works now is suitable only for toy examples.)
|
|