T
Thomas Hawtin
Stefan said:I have a class whose instances are both an A and a B:
interface A {}
interface B {}
class C implements A, B {}
The method »m« needs to declare that it returns somthing that
is also both an A and a B (but not necessarily a C).
I try to express this via the type paramter »T«:
<T extends A & B> T m(){ return new C(); }
It's the caller that determines exactly what T is. T need not be a
subtype of C. What you appear to be trying to do is:
A&B m() { return new C(); } // Illegal.
or
? extends A&B m() { return new C(); } // Still illegal.
I was hoping that the compiler (JDK 1.6 beta) would sse that
»new C()« satisfies this requirement. But the compiler
reports:
C does not always satisfy the requirements. Consider:
class D extends C {}
....
D d = YourClass. said:How could the code be modified to express that:
- m returns an object that implements (or »extends«) both A and B, and
- »return new C()« is accepted within m's declaration for this purpose?
You cannot use intersection types other than for generic parameters (and
implicitly as the result of the ?: operator).
Tom Hawtin