1
interface I1 { ... }
interface I2 { ... }
interface I3 { ... }
interface I4 { ... }

interface MyFactory {
  Object<? extends I1 & I2 & I3> createI1I2I3(); // doesn't work
  Object<? extends I2 & I3 & I4> createI2I3I4(); // doesn't work
}

Is there a trick to do it? I was thinking about things like

interface I1I2I3 extends I1, I2, I3 { ... }

But I1I2I3 != <? extends I1 & I2 & I3>. There's a reason I just can't use this approach - I1, I2 and I3 are foreign code.

Update

For those who curious why might someone need such a weird thing:

interface Clickable {}
interface Moveable {}
interface ThatHasText {}

interface Factory {
  Object<? extends Clickable> createButton(); // just a button with no text on it
  Object<? extends Clickable & ThatHasText> createButtonWithText();
  Object<? extends Moveable & ThatHasText> createAnnoyingBanner();
}
1
  • 2
    I got to learn the use-case for such a construct ... Commented Sep 26, 2011 at 18:55

2 Answers 2

2

Object doesn't accept type parameter You can use the following construct instead:

interface I1 {  }
interface I2 {  }
interface I3 {  }
interface I4 {  }

interface MyFactory {
    public <T extends I1 & I2 & I3> T createI1I2I3(); 
    public <T extends I2 & I3 & I4> T createI2I3I4(); 
}
Sign up to request clarification or add additional context in comments.

Comments

2

Your return type should be parameterized, so you can do

interface MyFactory {

   <T extends I1 & I2 & I3> T createI1I2I3();

}

Comments

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.