I want to define an interface MyList which is a list of interface MyThing. Part of the semantics of MyList is that its operations don’t have any meaning on objects which do not implement the MyThing interface.
Is this the right declaration?
interface MyList<E extends MyThing> extends List<E> { ... }
edit: (part 2) Now I have another interface that returns a MyList as one of its methods.
// I'm defining this interface
// it looks like it needs a wildcard or template parameter
interface MyPlace {
MyList getThings();
}
// A sample implementation of this interface
class SpecificPlace<E extends MyThing> implements MyPlace {
MyList<E> getThings();
}
// maybe someone else wants to do the following
// it's a class that is specific to a MyNeatThing which is
// a subclass of MyThing
class SuperNeatoPlace<E extends MyNeatThing> implements MyPlace {
MyList<E> getThings();
// problem?
// this E makes the getThings() signature different, doesn't it?
}
Yes, at least that is how
EnumSetdoes it.Edit in answer to Part 2:
I’m not sure why the return type of
getThings()in the interface doesn’t complain about raw types. I suspect that because of type erasure, warnings in interfaces would be useless even if they were there (there’s no warning if you change the return type toList, either).For the second question, since
MyNeatThingextendsMyThing,Eis within its bounds. That’s sort of the point of using theextendsbound in the generic parameter, isn’t it?