0

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){
...
}
8
  • 5
    They both already extend AbstractMap. That's a common base class. Commented Dec 21, 2016 at 13:48
  • As khelwood already says, they have a common ancestor. What do you hope to achieve by extending both? Commented Dec 21, 2016 at 13:50
  • Also, they implicitly extend Object. Commented Dec 21, 2016 at 13:50
  • They also both implement Map<T,T>. Commented Dec 21, 2016 at 14:00
  • Don't know your aim, why a common base class is needed. Maybe a common interface interface Base<T> could fulfill your needs, e.g. if its mentioned as something like a MarkupInterface. An implementation of such an interface should be compatible with the extensions of Maps subclasses. Commented Dec 21, 2016 at 14:04

3 Answers 3

2

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:

public class MyMap<T> extends AbstractMap<T,T> {
    private Map<T,T> delegate;

    public MyMap(Map<T,T> delegate) {
        this.delegate = Objects.requireNonNull(delegate);
    }

    public Set<Map.Entry<T,T>> entrySet() {
        return delegate.entrySet();
    }

    // Optionally, implement other Map methods by calling the same methods
    // on delegate.

    public T f(T o) {
        // ...
    }
}

and then:

public class A<T> extends MyMap<T> {
    public A() {
        super(new HashMap<>());
    }
}
public class B<T> extends MyMap<T> {
    public B() {
        super(new TreeMap<>());
    }
}

or simply:

Map<T,T> aMap = new MyMap<>(new SomeOtherMapImplementation(...));

But obviously, now A and B are not themselves subclasses of HashMap and TreeMap respectively, so if that's what you need, you're out of luck :-).

Sign up to request clarification or add additional context in comments.

Comments

1

As they both implement Map<T,T> you can do something like:

public class A<T> extends HashMap<T, T> {
}

public class B<T> extends TreeMap<T, T> {
}

List<Map<String,String>> list = Arrays.asList(new A<String>(), new B<String>());

1 Comment

That's an answer to the question: "Is it possible to hold references of A<T> and B<T> in one data structure." That answer targets in now way to the named problem with a common base. Beside that, it's correct. You can do this.
0

I think you should create an Abstract class with the method that you want. And then instead os extends HashMap and TreeMap, use this data structures as fields of your new classes depending on your needs.

For instance:

public abstract class MyClass<T> {
   public T f(T o){
       ...
   }
}

public class A<T> extends MyClass<T> {
    private Map<T,T> mapThatINeed;
}

Comments

Your Answer

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.