File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes why Properties extends Hashtable with Object parameter type instead of String? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "why Properties extends Hashtable with Object parameter type instead of String?" Watch "why Properties extends Hashtable with Object parameter type instead of String?" New topic
Author

why Properties extends Hashtable with Object parameter type instead of String?

Zemian Deng
Greenhorn

Joined: Jun 12, 2007
Posts: 21
Does anyone know why definition of java.util.Properties extends Hashtable<Object, Object> instead of Hashtable<String, String>? As stated by javadoc, Properties can and should only hold String right?

Also, is there better way to process a Properties entries without cast?


Thanks,
Z
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 23635

Zemian,
If the subclass used different parameter types, it wouldn't override the method. This goes back to the pre Java 5 days. When generics were introduced, they tried to keep backward compatibility with existing code that was using the Object API.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Certs: SCEA Part 1, Part 2 & 3 & Core Spring 3, OCAJP
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18365

To be honest, I agree with Zemian. The only reason why old code would break if it were a Hashtable<String,String> would be if programmers put Objects directly into the Properties object using the put method. IMHO, that would mean using a Properties object in a way it wasn't meant to be used.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Yes, but the thing it, it is possible that people used put(Object, Object). Illogical as that may be, it's perfectly legal. It would have been better if Properties did not extend Hashtable in the first place - it should have contained one instead. But it's one of those bad design decisions that Sun made long ago, and now we're stuck with it. And Sun does take their backward compatibility very seriously, even here.


"I'm not back." - Bill Harding, Twister
Zemian Deng
Greenhorn

Joined: Jun 12, 2007
Posts: 21
Right, backward compatibility. I was going to guess that. Well, I am glad some of you agree what I found. It just bugs me that with new Generic added, I still can't loop a Properties without casting.

Thanks for sharing your thoughts.
Cheers,
Z
 
 
subject: why Properties extends Hashtable with Object parameter type instead of String?
 
Threads others viewed
How to Handle Properties
Maintaining the order in a collection.
Get Property Values from Properties.Load()
Properties class
Read files and replace Strings
MyEclipse, The Clear Choice