Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

type combinations

Thread view: 
Stefan Ram - 29 Apr 2006 22:34 GMT
 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(); }

 I was hoping that the compiler (JDK 1.6 beta) would sse 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?
Thomas Hawtin - 29 Apr 2006 22:00 GMT
>   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!
s.Siddharth.Sharma@gmail.com - 29 Apr 2006 23:07 GMT
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?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.