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 / March 2007

Tip: Looking for answers? Try searching our database.

Downcasting from generic bases

Thread view: 
Christopher Benson-Manica - 14 Mar 2007 20:33 GMT
Why isn't this legal?

public class TestIt {
 private static class Foo<T> {
 }

 private static class Bar extends Foo<Integer> {
 }

 private static void Baz( Foo<?> foo ) {
   if( foo instanceof Bar ) { // "inconvertible types" error
   }
 }
}

And since that's not legal, why exactly does it become legal if the
parameter foo is of the raw type Foo?  I clearly don't understand
generics completely and it seems perverse that I can't ask a
superclass if it's an instance of a base class just because of the
generic type paramters.

Signature

C. Benson Manica           | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com      | don't, I need to know.  Flames welcome.

Joshua Cranmer - 15 Mar 2007 01:03 GMT
> Why isn't this legal?
>
[quoted text clipped - 16 lines]
> superclass if it's an instance of a base class just because of the
> generic type paramters.

Generics are sometimes confusing. Here is why it gives you an error:

private class Foo<T> {
   protected T doSomething();
}

private class Bar extends Foo<Integer> {
   protected Integer doSomething();
}

foo1 = new Foo<Integer>();
foo2 = new Bar();
foo1.doSomething();
foo2.doSomething();

Erasing the types, the first method will return Object and the second
Integer. Then, foo2's doSomething will become ambiguous in your code.
The return type would become ambiguous because of the cast.

Yes, I know it seems overprotective, but much of generics is like that.
Tom Hawtin - 15 Mar 2007 02:08 GMT
> Why isn't this legal?
>
[quoted text clipped - 10 lines]
>   }
> }

Works for me! Are you using and old or non-Sun compiler?

There are some confusing wildcard situations where you have nesting of
generic types (Foo<Bar<?>>), but this code should not be a problem.

Tom Hawtin
Christopher Benson-Manica - 15 Mar 2007 13:53 GMT
> Works for me! Are you using and old or non-Sun compiler?

No.  I mean, yes, but I didn't realize it until just now.  Oops! :-)
(I was using 1.5.0_06 - did the language spec change or was this
merely a bug that was fixed in a later version?)

Signature

C. Benson Manica           | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com      | don't, I need to know.  Flames welcome.



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.