I have an abstract java class that looks like:
public abstract class X {
public abstract void commonFunction();
}
I also have a bunch of maps, that I would like to be like this:
Map<String,A> mapA = new HashMap<String,A>();
Map<String,B> mapB = new HashMap<String,B>();
Map<String,C> mapC = new HashMap<String,C>();
Map<String,D> mapD = new HashMap<String,D>();
Map<String,E> mapE = new HashMap<String,E>();
A, B, C, D and E all extend X so they all implement commonFunction().
I'd like to be able to call a function like this:
someFunction(mapA); // or someFunction(mapB) etc.
...where someFunction looks like:
void someFunction(Map<String,X> mapX) {
...some stuff
x.commonFunction();
...more stuff
}
But the compiler complains with the things I've tried, which includes:
- changing the map declarations (left-hand side) by replacing [ABCDE] with X
- casting the map into
Map<String,X>inside thesomeFunctionfunction call
Also tried with an implemented interface instead of an extended class. Not sure what I'm doing wrong.
Edit:
I'm sorry, I rushed the question so I made a mistake. My maps are actually:
Map<String,List<A>> mapA = new HashMap<String,List<A>>();
...and so the someFunction signature is actually:
void someFunction(Map<String,List<X>> mapX)
I'm not sure if that makes a big difference.