This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Question for Herb (or anyone else) on Generics and unknown object types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question for Herb (or anyone else) on Generics and unknown object types" Watch "Question for Herb (or anyone else) on Generics and unknown object types" New topic
Author

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

Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

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?


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
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

Joined: Aug 20, 2001
Posts: 1821

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 ]
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
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
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
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.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
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?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question for Herb (or anyone else) on Generics and unknown object types