• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritence or Composition?

 
umar hathab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to use an object as a hashmap.I dont want any other functionality fo hashmap and I will just use my object to store key,pair values.I heard that its better to use composition than my object extends hashmap.Can any one tell me how extending the hashmap affects performance than using hashmap as composition...
(will it affect memory,time taken,etc...)
This may be a basic ques...but please help me...

A.Umar
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't a performance question. The only reason to subclass an object is to add additional functionality to the object (or, occasionally, override existing functionality). Since you don't require additional functionality, you should not be extending the class.
--Mark
 
Hugo van Elk
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by umar hathab:
Hi,

I want to use an object as a hashmap.I dont want any other functionality fo hashmap ...
A.Umar

Hi Umar
If you do not need other functionality, why don't you just instantiate java.util.HashMap? You don't need to subclass or wrap it...

CUU
/Hugo.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also think you are missing some of the point of the carefully constructed Collections API. It sounds as if you actually want your object to exhibit some of the properties of a Map, not specifically a HashMap. HashMap is just one of the implementation options - you could also choose Hashtable, Properties, TreeMap, WeakHashMap etc.
If you write your code to use Map, then you are free to initialize your Map variables by constructing any of the above classes, or any subclass of them, or any class you make which implements the Map interface.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!