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

Tip: Looking for answers? Try searching our database.

Generics references

Thread view: 
jupiter - 28 May 2007 18:28 GMT
I've noticed that the type in generic declarations is not
"inherited" by new references to the object.  This seems
inconsistent to me but maybe there's a good reason for it.  To wit:

ArrayList<String> list1 = new ArrayList<String>();

ArrayList list2 = list;

list1.add(new Integer(3)); //compiler catches it as expected.
list2.add(new Integer(3)); //compiler is fat, dumb and happy.

I thought the idea of a reference was to point to the original
object and pick up all of the relevant data. Why not type?
Mark Rafn - 29 May 2007 17:10 GMT
>I've noticed that the type in generic declarations is not
>"inherited" by new references to the object.  This seems
>inconsistent to me but maybe there's a good reason for it.

Type inference is intentionally weaker than it could be.  There are good
reasons for it, I suspect, but it annoys me sometimes.

To wit:
>ArrayList<String> list1 = new ArrayList<String>();
>ArrayList list2 = list;

List2 is explicitly NOT declared as a parameterized type.  It happens to have
an ArrayList<String> in it now, but you could assign it an ArrayList<Object>
later without error.  

>list1.add(new Integer(3)); //compiler catches it as expected.
>list2.add(new Integer(3)); //compiler is fat, dumb and happy.

Right, because you told it to be.

>I thought the idea of a reference was to point to the original
>object and pick up all of the relevant data. Why not type?

Type is tricky.  It's both a property of the variable and a property of the
referent.  The object type can be more specific than the variable, and that's
how polymorphism works.

Note the similarity to:
LinkedList list1 = new LinkedList();
List list2 = list1;
list1.addFirst("first"); // allowed, addFirst is a method on LinkedList
list2.addFirst("first"); // disallowed, addFirst is NOT on the List interface
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>
jupiter - 30 May 2007 01:42 GMT
>>I've noticed that the type in generic declarations is not
>>"inherited" by new references to the object.  This seems
[quoted text clipped - 27 lines]
> variable, and that's
> how polymorphism works.

Great piece of insight, Mark. I never differentiated between type
and other properties, so this is nice piece to become aware of.

> Note the similarity to:
> LinkedList list1 = new LinkedList();
[quoted text clipped - 3 lines]
> list2.addFirst("first"); // disallowed, addFirst is NOT on the
> List interface

Nice example indeed.   This is yet another case of how implicit
casting can cause headaches for we normally-brained people.


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.