1

I'm trying to do something like the following, but the compiler complains when I do:

class A {}

interface B {}

ArrayList<? extends A implements B> list;

I've also tried

ArrayList<? extends A & B> list;

Why isn't this legal? The Java docs specify an example like this

class C<T extends A & B> {}

So why not allow it in a type definition?

Mark
  • 1,746
  • 2
  • 18
  • 19
  • you have to define A as class A implements B{}, simply saying A implements B doesn't make it true when you define the type inside the ArrayList. – RAZ_Muh_Taz Apr 10 '17 at 22:36
  • 1
    @RAZ_Muh_Taz - But a subclass might. – Oliver Charlesworth Apr 10 '17 at 22:37
  • @RAZ_Muh_Taz 1) I don't have control over class A, it's a class in an API I'm given; 2) I'm not trying to say that class A implements interface B, I'm trying to specify that every object in the ArrayList both extends class A and implements interface B, similar to what's seen in the Oracle example. – Mark Apr 10 '17 at 22:42
  • perhaps A is not defined first as stated in the last bit of the Java doc you linked? In your example it is but perhaps in the actual code it is not? @Mark – RAZ_Muh_Taz Apr 10 '17 at 22:48
  • @RAZ_Muh_Taz in my code the class is listed first in the generics, as specified in the documentation. Additionally, I was able to get the example in the documentation to work with my actual class and interface, but I can't get something like what's in my question to work – Mark Apr 10 '17 at 22:50
  • @shmosel sorry for asking a duped question, I searched but didn't see any question that had an answer to this. Can you show me the question that this one is duplicating? – Mark Apr 10 '17 at 22:52
  • 1
    It's linked up on top. – shmosel Apr 10 '17 at 22:52
  • @shmosel thanks, I only saw the 'marked as duplicate' comment at the bottom and didn't notice the link at the top. I guess it shows I don't spend a ton of time on this site. Thanks for the link! – Mark Apr 10 '17 at 23:09

0 Answers0