> I have a class whose instances are both an A and a B:
>
[quoted text clipped - 7 lines]
>
> <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.<D>m();
> 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

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Stefan Ram - 29 Apr 2006 23:17 GMT
>What you appear to be trying to do is:
> A&B m() { return new C(); } // Illegal.
That is indeed what I'd like to express.
>C does not always satisfy the requirements. Consider:
>class D extends C {}
> D d = YourClass.<D>m();
I see.
Thanks for both the answers to my post!
hi Stefan,
You can only return something that is both an A and B by returning an
object of a class which implements these both interfaces given that
both A and B are non related.
The compiler is giving you error because T can be given any data type
same or different than C. Hence, the declared returning data type (T)
might not fall in the same type heirarchy of C.
Regards,
Sidd
> I have a class whose instances are both an A and a B:
>
[quoted text clipped - 22 lines]
> - 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?