Take the 2-minute tour ×

Ok guys, here's a simple question that I couldn't quite manage to figure out on my own. Any help is greatly appreciated!

  • Let's say I have an abstract class Superclass, from which I derived subclasses Subclass1 and Subclass2.
  • Let's say I have another myClass class, with a myField field. I would like to specify that myField's type should be a List of a fixed subclass of Superclass, i.e. either List<Subclass1> or List<Subclass2>.

How should I type myField? List<Superclass> doesn't work since such a list could theoretically contain a combination of Subclass1 and Subclass2 objects... What I'm really looking for here is something like List<subclass(Superclass)>. Does that even exist? How would you go about this?

Again, thanks a lot for the help!


share|improve this question
Where would you specify which type it is? –  Oli Charlesworth Sep 5 '11 at 0:35
Well if you only want classes of type Subclass1 you'll have to specify exactly that. Note that List<Superclass> wouldn't be correct (except if Superclass could be instantiated itself), it should be List<? extends Superclass> if you wanted all subclasses of Superclass - but since that's not what you want, it doesn't really matter.. –  Voo Sep 5 '11 at 0:39
add comment

3 Answers

up vote 1 down vote accepted

You need to use generics.

class MyClass<S extends SuperClass> {
  private List<S> myField;

Hope this helps.

share|improve this answer
Generics are only enforced at compile-time. If he wants the class to accept either so long as they're all the same, he would need runtime checking. –  jiggy Sep 5 '11 at 0:45
@jiggy compile-time checking will be sufficient if all code that will run uses generics. Also he wants the list to contain instances of only a single subclass at a time. –  shams Sep 5 '11 at 3:58
Generics is exactly what I'm looking for. Thanks! –  guidupuy Sep 5 '11 at 6:59
add comment

You could use a bounded wildcard:

class MyClass {
    List<? extends Superclass> myField;

If you do it this way, you cannot call methods such as add on myField (well, you can, but you can only pass in null.

An alternative is a generic class:

class MyClass<T extends Superclass> {
    List<T> myField;
share|improve this answer
add comment

What you look for is in fact a run time check, what you can do is, declare myField as ,

private List<Superclass> myField

and have a setter to do the run time validation;

public addMyField(Superclass field){

   if (field instanceof Subclass1 || field instanceof Subclass1){
share|improve this answer
That's not what the OP is looking for. –  Oli Charlesworth Sep 5 '11 at 0:44
@Prabath guidupuy is looking to have instances of only one of those subclasses in his list –  shams Sep 5 '11 at 3:59
You cannot do it in compile time. It should be a runtime check to fiter out the exact type u need. That's what the answer suggests. –  Prabath Siriwardena Sep 5 '11 at 4:25
If you need an exact type of a non-final class then runtime checking is required, but that is not what the original question is about! –  shams Sep 5 '11 at 22:53
add comment

Your Answer


