I'm working on a project that has an extensive tree of generic inheritance and dependencies. Go to edit to see 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 StringMap
type.
class X {
StringMap<D<C>> _thing = new StringMap<D<C>>;
...
}
So far this all works fine. D<C>
is actually a very long name and the specific combination is going to show up very frequently in other parts of the code, so I decided to a class for the specific combination so it will be clearer and have a shorter name.
class DC extends D<C> {
}
//and go to update X
class X {
StringMap<D<C>> _thing = new StringMap<D<C>>(); //still works fine
StringMap<DC> _thing = new StringMap<DC>(); //error
...
}
Eclipse gives the error of
Bound mismatch: The type
DC
is not a valid substitute for the bounded parameter<T extends A>
of the typeStringMap<T>
So the question is, why does this not just work? DC
does nothing but extend D<C>
and echo the constructors. Why does StringMap
see DC
as different when it is just a child class of something it excepts?
EDIT:
OK, reworked the example to be closer to what I'm actually doing. I tested it and it does produce the error. What I'm doing here is using the generic type to ensure that clone()
returns the correct class for whoever implements it down the inheritance tree. Then in subclasses, I'm using B<T extends B<T>>
to ensure that subclasses of B
are passing in a subclass of B as the 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()
{
// TODO Auto-generated method stub
return null;
}
@Override
public void updateFields(T modified)
{
// TODO Auto-generated method stub
}
@Override
String getKey()
{
// TODO Auto-generated method stub
return null;
}
}
public class C extends B<C> {
}
public class D<T extends B<T>> extends A<D<T>, String> {
@Override
String getKey()
{
// TODO Auto-generated method stub
return null;
}
@Override
public D<T> clone()
{
// TODO Auto-generated method stub
return null;
}
@Override
public void updateFields(D<T> modified)
{
// TODO Auto-generated method stub
}
}
public class DC extends D<C> {
}
public class StringMap<T extends Undoable<T>> {
HashMap<String, T> _elements;
}
public class Main {
public static void main(String[] args)
{
StringMap<D<C>> _thing = new StringMap<D<C>>(); //works
StringMap<DC> _thing1 = new StringMap<DC>(); //error
//Bound mismatch: The type DC is not a valid substitute for
//the bounded parameter <T extends Undoable<T>> of the type StringMap<T>
}
}