Let's have two classes in Java (7):
public class A<T> extends HashMap<T, T>{
...
}
and
public class B<T> extends TreeMap<T, T>{
...
}
Is it possible to have a common base class which these two classes would extend?
Thanks.
Clarification: I want that the classes share the same method
public T f(T o){
...
}
No, that is not possible. Java does not support multiple inheritence, so each class can only extend a single class. Since both of your classes already extends a different class, you cannot create a class that is a superclass of both of your classes.
A possible solution is to use composition:
and then:
or simply:
But obviously, now
A
andB
are not themselves subclasses ofHashMap
andTreeMap
respectively, so if that's what you need, you're out of luck :-).