given:
static class ClassA {
static <V> ClassA create( List<V> things ) {
//do something with things
return new ClassA();
}
}
static class ClassB extends ClassA {
//note: using <V> is fine, but <V extends Number> gives an error
static <V extends Number> ClassB create(List<V> numbers) {
//do something with numbers
return new ClassB();
}
}
I am getting the error:
'create(List)' in 'ClassB' clashes with 'create(List)' in 'ClassA'; both methods have same erasure, yet neither hides the other
Easy way around this problem is to just name the methods differently, but I want to better understand why this is a problem (and if there are workarounds).
From my understanding of static methods, the method to call is picked based on the static type of the object. so ClassA foo = new ClassB(); foo.create(null);
would make a call to ClassA.create(null)
. And of course, there shouldn't be a problem with picking the right method when using the class name directly: ClassB.create
or ClassA.create
.
My guess is that it would have a problem with ClassB.create(new ArrayList<String>())
. Though at compile time, it would still be possible to determine that ClassA.create
could (should(?)) be called.