I am working on a project with an extensive tree of common inheritances and dependencies. Go to edit to see a better example. The basics look something like this:
class A { ... } class B { ... } class C extends B { ... } class D<T extends B> extends A { ... } class StringMap<T extends A> { HashMap<String, T> _elements; ... }
So now I'm going to write a class that contains a specific type of StringMap .
class X { StringMap<D<C>> _thing = new StringMap<D<C>>; ... }
So far, everything is working fine. D<C> is actually a very long name, and a particular combination will be displayed very often in other parts of the code, so I decided the class for a particular combination to be more clear and have a shorter name.
class DC extends D<C> { }
Strike>
Eclipse gives an error
Associated mismatch: DC type is not a valid replacement for the limited parameter <T extends A> type StringMap<T>
So the question is, why doesn't this just work? DC does nothing but extend the D<C> and echo constructors. Why StringMap see DC as excellent when it is only a child class of something other than it?
EDIT:
Well, I reworked the example to be closer to what I'm actually doing. I tested it and it causes an error. What I'm doing here uses a generic type to ensure that clone() returns the correct class for those who implement it in the inheritance tree. Then in subclasses I use B<T extends B<T>> to ensure that subclasses of B are passed in subclass of B as a generic type T
public abstract class Undoable<T> implements Comparable<T> { public abstract T clone(); public abstract void updateFields(T modified); } abstract public class A<T extends A<T, U>, U extends Comparable<U>> extends Undoable<T> { abstract U getKey(); @Override public int compareTo(T element) { return getKey().compareTo(element.getKey()); } } public class B<T extends B<T>> extends A<T, String> { @Override public T clone() {
java generics type-erasure restriction
unholysampler
source share