• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question for Herb (or anyone else) on Generics and unknown object types

 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been reading over the Generics tutorial and have noticed that they seem to impare the ability to add new unknown-types objects to a list of unknown-but-similarily-typed objects.

How would I do this? An example of what I am doing in 1.4 is below:



Basically, I am taking a list of objects of unknown type (List<?> in generics) and, using reflection, creating a new object that I then add to the list. However, as per the tutorial, you can't add objects into a List<?>; you can only get from.

These objects are not able to be subclassified any further than Objects (other than they have to have a <T>() constructor for the reflection-based instantiation to work....) They could be customer obejcts or java.io.File objects or anything else.

Since Generics is enforced by the compiler, I suppose that I could use reflection to get around this, but that seems a bit like cheating. Is there some way to do this unsing generics?
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use List<Object>. But I agree, I'd rather see it possible to simply use List in that situation, without having to specify the <Object> part.
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah...Id just have to write a conversion method like:



You can't overload methods with generics, can you? Since they're not compile time, I couldn't also have:


becasue in both cases the signature is the same, correct?
[ August 27, 2004: Message edited by: Joel McNary ]
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's all part of the type-safety argument isn't it? It forces you to make your intent explicit, i.e. that your List can take any Object. The main issue that I see with generics in Beta 2 is that you can't get around the warnings for legacy Collections code. If the final release includes a compiler switch to allow for that (specifically enough) then there's effectively no difference.

Jules
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This dicussion thread points out what I have been saying about how generics changes the Java langauge. Generics also change how we think about the programming solutions that we implement. Some techniques, such as a list of unknown objects, are still possible, but the addition of generics points us away from thinking in these terms. Generics encourage us to think in terms of a specific type, or set of types related by a common superclass (other than Object, of course).

I think that it will take the Java community a year or more to fully come to terms with the implications of generics.

Java is changing, and we will have to change with it.
 
Warren Dew
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Herb Schildt:

Generics also change how we think about the programming solutions that we implement. Some techniques, such as a list of unknown objects, are still possible, but the addition of generics points us away from thinking in these terms.

I'm seeing some stuff between the lines, here - but I'm wondering if what I'm seeing is really there.

This could be read as saying that there's a fundamental shift away from Smalltalk style late binding and weak(er) typing, and towards C++ style early binding and strong(er) typing. Put another way, it would be a shift away from an extremely object oriented paradigm, and toward classes as primarily a way of managing code.

Do you think that's true?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Warren Dew:
This could be read as saying that there's a fundamental shift away from Smalltalk style late binding and weak(er) typing, and towards C++ style early binding and strong(er) typing.



I think you mixed terms here. Smalltalk is strongly and dynamically typed, C++ weakly and statically.

Put another way, it would be a shift away from an extremely object oriented paradigm, and toward classes as primarily a way of managing code.



I don't think this is true at all. The most important feature of OO languages is polymorphism, and Tiger isn't changing that. On the other hand I would say that polymorphism *is* a tool for managing code (that is, code dependencies).
 
reply
    Bookmark Topic Watch Topic
  • New Topic