With generic methods it’s possible to extend more than one type, e.g.:
<T extends MyClass & MyInterface> void foo(T bar)
Is there a way to specify a List with a parameter which extends more than one type?
List<MyClass & MyInterface> myList;
doesn’t work…
This would allow the following:
class A extends MyClass implements MyInterface{}
class B extends MyClass implements MyInterface{}
myList.add(new A());
myList.add(new B());
MyClass c = myList.get(index);
MyInterface i = myList.get(index);
foo(myList.get(index));
The answer is NO.
Depending on your semantic expectation you will find a workaround
One possible workaround for foo
Probably the best approach is to create a type supplying both type.
The drawback is, all interesting classes needs to derived from that class
The naive approach just to supply both methods will cause ambiguities, so it is not a possible (for “AND”, for “XOR” it would be valid)