S
Stefan Ram
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 something that
is also both an A and a B (but not necessarily a C).
I try to express this via the type parameter »T«:
<T extends A & B> T m(){ return new C(); }
I was hoping that the compiler (JDK 1.6 beta) would see that
»new C()« satisfies this requirement. But the compiler
reports:
incompatible types
found : C
required: T
{ return new C(); }
1 error ^
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?
interface A {}
interface B {}
class C implements A, B {}
The method »m« needs to declare that it returns something that
is also both an A and a B (but not necessarily a C).
I try to express this via the type parameter »T«:
<T extends A & B> T m(){ return new C(); }
I was hoping that the compiler (JDK 1.6 beta) would see that
»new C()« satisfies this requirement. But the compiler
reports:
incompatible types
found : C
required: T
{ return new C(); }
1 error ^
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?