1

Have a template class:

class Foo <T> {

    private T bar;

    public Foo(T bar) {
        this.bar = bar;
    }

}

Have an interface:

interface SomeInterface {
    public void boom();
}

I want Foo to only accept types of classes that implement the interface SomeInterface.

Intuitively, I wanted something like this:

class Foo <T implements SomeInterface>

But apparently this doesn't work.

Is it possible for a template class to only accept implementers of a specific interface?


This is because I want to call certain methods on this.bar. Methods that happen to be defined in SomeInterface.

Saturn
  • 17,888
  • 49
  • 145
  • 271
  • 9
    Use `extends` instead of `implements` – awksp May 31 '14 at 05:31
  • 1
    @user3580294: Thank you. That wasn't intuitive. – Saturn May 31 '14 at 05:32
  • 3
    No problem. And in case it's helpful later, you can specify multiple bounds for a type parameter using `&`: ``. Only restriction is that if there's a class in there it has to be listed first (I think), and a type can only extend one class. – awksp May 31 '14 at 05:35

0 Answers0