I’m pretty sure I can solve this problem with Java generics so I wanted to ask…Let’s say I have a few classes that each implement a common interface, there’s a method in the common interface called getData, which I’d like to make generic. The problem I have is that each class returns a different object type, so ideally I’d like these to be generic, then in the implementation class that calls getData, convert them back to their original types. If I were to cast to an Object and back to specific types I can see how that would work, but can I accomplish the same end result with Generics?
Here is some pseudo code to outline the problem:
public ClassA implements interface x {
private CustomObjectA customObjectA;
private Map<DateTime, Map<String, CustomObjectA>> dataMap;
...
public Map<DateTime, Map<String, CustomObjectA>> getData() {
return this.dataMap;
}
}
public ClassB implements interface x {
private CustomObjectA customObjectB;
private Map<DateTime, Map<String, CustomObjectB>> dataMap;
...
public Map<DateTime, Map<String, CustomObjectB>> getData() {
return this.dataMap;
}
}
public interface x {
public Map<DateTime, Map<String, genericObject>> getData();
}
public ClassC {
public testGeneric(x objX) {
CustomObjectA cOA = objX.getData();
CustomObjectB cOB = objX.getData();
}
}
What you probably want is a generic interface :