I have a programming problem in Java:
Can I define a different method for any different interface array element?
myInterface[] op = new myInterface[4];
Now “myInterface” only has 1 method: public static int doSomething(int a, int b);.
I need to define that method to do different things with the numbers based on the element id (0-3).
Restrictions:
- must use hash tables.
- no switch-case or if statements (because of hash tables)
- cannot under any circumstance use the Java defined hash tables
i.e. if I call op[0].doSomething(2,3) it’ll output 12 (2^2 * 3)
and if I call op[1].doSomething(2,3) it’ll output 5 (2+3).
EDIT: I’d like to know if its possible or am I chasing my tail?
if it is could someone suggest a way for it to be done, I don’t need the entire answer just suggestions, please.
P.S. Also I looked at all the pages on interfaces in Java, haven’t found anything helpful.
I got it.
Answer: use anonymous classes.