Re: Generics references



jupiter <jupiter49byebyeSpam@xxxxxxx> wrote:
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@xxxxxxxxx <http://www.dagon.net/>

.



Relevant Pages

  • Re: Generics references
    ... "inherited" by new references to the object. ... inconsistent to me but maybe there's a good reason for it. ... List2 is explicitly NOT declared as a parameterized type. ... LinkedList list1 = new LinkedList; ...
    (comp.lang.java)
  • Re: Trying to avoid listing bad supervisors on job apps
    ... When I explained that we butted heads frequently and that he often overruled any advice from me that he didn't like, they accepted the references that I did submit. ... let me ask you if you know what the overall applicant to job ratio was and how many people were called in for an interview. ... In today's climates, lots of recruiting efforts end up with the sellection process throwing out the whole applicant pool and for very odd reasons or no reason at all. ... I actually had two supervisors who wrote or otherwise gave "average" type references. ...
    (sci.research.careers)
  • Strange dream last night.
    ... Last night I dreamed I was watching a remix version of 'The Parting of ... The editors had removed all references to the Rose/vortex deus ex ... Eccleston was suddenly replaced by Tennant for no reason at all either. ...
    (rec.arts.drwho)
  • Re: Problem with own global operator+
    ... > references to dynamically allocated memory. ... I cannot create a typecasting constructor, since "sum" is defined ... The reason is that you allocate objects with new, ...
    (comp.lang.cpp)
  • Re: My definition of force!!!
    ... > souls who refer others to websites. ... reason you want this is that you are having trouble following the ... containing a plethora of facts and opinions. ... references are long for a reason -- it takes a bit to explain things ...
    (sci.physics)