0
public class AClass<X extends AnInterface>{

    public AClass(X x){
    }

}

public class Y implements AnInterface{

}

In the calling code if I have:

public static<X extends AnInterface> void main(String args){

    AnInterface y = new Y();
    AClass a = new AClass<AnInterface>(y);

}

I should be able to get this to work using X in the constructor for AClass right? I am getting errors telling me the contructor for AClass should be of type AnInterface??

EDITED to change AClass a = new AClass<AnInterface>(y);

2 Answers 2

2

You need to tell AClass what type its expecting.

    public static void main(String args){
        AnInterface y = new Y();
        AClass<AnInterface> a = new AClass<AnInterface>(y);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I do have that- I had to re-type my code out here and missed that bit. Its complaining about the type of the constructor for AClass.
Look at my code and see if you can spot the difference :) You need to declare a as AClass<AnInterface> too. Thing is, I suspect there is a better solution to your problem although can't tell from this example.
0
public static void main(String[] args) {
    AnInterface y = new Y();
    AClass a = new AClass(y);
}

1 Comment

That doesnt get rid of the warning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.