2

I has a Args class,this class extend HashMap and has some methods:

public class Args extends HashMap{
 public myMethod_1(){
 //...
 }
 public myMethod_2(){
 //...
 }
 public myMethod_3(){
 //...
 }
}

now,I want to get a Linked Args instance,but exnted LinkedHashMap,

I can't change Args exnteds to LinkedHashMap,also can't change Args to a interfaces

can I get a linked Args instance ?

if not,how the best way to get a LinkedArgs? (not repeat myMethod_1-3)

asked Mar 30, 2011 at 2:25
2
  • 2
    Why can't you change Args to extend LinkedHashMap? Do you realize that LinkedHashMap extends HashMap? So by changing Args to extends LinkedHashMap, you're not giving up any functionality provided by HashMap. Commented Mar 30, 2011 at 2:33
  • 2
    Can you clarify what you're going for? I don't understand why you cannot extend LinkedHashMap instead of just HashMap. Commented Mar 30, 2011 at 2:36

1 Answer 1

2

The simple solution is change Args to extend LinkedHashMap. (I know you said you can't ...)

If that is not an option, then the next best solution is to change Args to implement Map and delegate all of the methods in the Map API (and others as required) to a private LinkedHashMap instance.

If that is not an option, you could do the same with extends HashMap. Basically, you override all of the methods in the parent HashMap, and delegate the actions to a private LinkedHashMap instance. (It is UGLY but it works ... modulo that each Args object has a bunch of attributes that are initialized and never used.)

answered Mar 30, 2011 at 2:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.