1

I'm stuggling for some hours with this problem and I hope that you can help me out.

I have a interface which defines some methods:

public interface LanguageInterface {
         //...
}

There is a class which implement the LanguageInterface

public class ZPL implements LanguageInterface {
    // ...
}

And now I want to create an enum which contains all those classes.

public enum PrintingLanguage {
   ZPL(ZPL.class);

   private Class<LanguageInterface> clazz;

   PrintingLanguage(Class<LanguageInterface> clazz) {
      this.clazz = clazz;
}

And now I always get an eclipse which says

The constructor PrintingLanguage(Class< ZPL >) is undefined

I simply want to allow the constructor to accept only classes which implements this interface.

How can I make this work?

Thanks in advance!

1 Answers1

5

You want Class<? extends LanguageInterface>, to allow any type parameter that extends the interface.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • interface and extends work together ? should LanguageInterface be a class and not an interface ? – StackFlowed Oct 03 '14 at 15:05
  • @wrongAnswer: Generic variance doesn't distinguish between interfaces and classes. – SLaks Oct 03 '14 at 15:06
  • 1
    @user2948795 if this is the correct answer please mark it as correct when you can i think its after 15 mins you asked the question ... – StackFlowed Oct 03 '14 at 15:11