type combinations

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top