0

Is it possible to specify multiple interfaces for a generic type in Java?

Specifically, I am working with an existing library (NASA World Wind) that has a number of interfaces for its objects. I have a set of heterogeneous objects I am working with that I would like to store in a list. All of the objects implement multiple interfaces, each of which is useful to me (the self-descriptive Movable and Renderable, specifically).

Is there some syntax I am missing to do this or is it not allowed? Do I have to pick one and cast to the others?

Phoebe
  • 2,774
  • 3
  • 22
  • 27
  • Possible duplicate: http://stackoverflow.com/questions/5034231/how-to-use-multiple-upper-bounds-in-generics – torquestomp May 15 '13 at 15:46
  • That is a similar, but not the same, question. It does look like it will get me what I need, but it has the side-effect of making the containing class generic on that type; that's not really desired. Is there no way to do it otherwise? – Phoebe May 15 '13 at 15:49
  • Just declare the generic type at the method level, i.e: `public void doStuff(T thing) {` – torquestomp May 15 '13 at 15:56
  • It does need to be declared at the class level, to be used in multiple methods. I think the original solution is probably the one to go with. – Phoebe May 15 '13 at 15:59

3 Answers3

3

If possible, define a common superinterface that all the others extend from. Then, the list will be declared to contain objects of that type.

Another option would be to specify at the class declaration level, that multiple upper bounds are possible, and declare the list attribute accordingly:

public class Foo<T extends Interface1 & Interface2> {
    private List<T> theList;
}

The above works as long as the type T implements both interfaces.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • It's not possible to define a common interface unless Java starts implementing duck typing (or unless I fork World Wind and edit a few dozen classes ;). – Phoebe May 15 '13 at 15:55
2

Look at Angelika Langer's FAQ: What is the difference between a wildcard bound and a type parameter bound?.

She gives the example:

class Box< T extends Comparable<T> & Cloneable >  
    implements Comparable<Box<T>>, Cloneable {...}

Here, T extends Comparable<T> & Cloneable is a type parameter bound, and states that T is comparable and cloneable. The general form is:

TypeParameter extends Class & Interface1 & ... & InterfaceN

After compilation, when types are erased, the type parameter devolves into the first type. T above is erased into Comparable<T>.

So, suppose you write:

public class HurricanePattern<T extends Movable & Renderable> {
    private T eye;
    // ...
}

After type erasure, the runtime type of eye is Movable.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Eric Jablow
  • 7,874
  • 2
  • 22
  • 29
0

You can create your own interface, as a wrapper, which extends several interfaces:

public MyInterface extends Comparable,Serializable,... {};
public class Foo<T extends MyInterface> {...}
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
  • That only works if you can edit all of the implementing classes (see other answers/comments). – Phoebe May 15 '13 at 16:10