I have an abstract class that has a generic method and I want to override the generic method by substituting specific types for the generic parameter. So in pseudo-code I have the following:
public abstract class GetAndParse {
public SomeClass var;
public abstract <T extends AnotherClass> void getAndParse(T... args);
}
public class Implementor extends GetAndParse {
// some field declarations
// some method declarations
@Override
public <SpecificClass> void getAndParse(SpecificClass... args) {
// method body making use of args
}
}
But for some reason I'm not allowed to do this? Am I making some kind of syntax error or is this kind of inheritance and overriding not allowed? Specifically I'm getting an error about @Override
because the eclipse IDE keeps reminding me to implement getAndParse
.
Here's how I want the above code to work. Somewhere else in my code there is a method that expects instances of objects that implement GetAndParse
which specifically means that they have a getAndParse
method that I can use. When I call getAndParse
on that instance the compiler checks to see whether I have used specific instances of T
in the proper way, so in particular T
should extend AnotherClass
and it should be SpecificClass
.
Static method can't override
Private method can't override
Final method can't override
What we are having here is two different methods with individual type parameters each.
This is a method with a type parameter named T, and bounded by
AnotherClass
, meaning each subtype ofAnotherClass
is allowed as a type parameter.This is a method with a type parameter named
SpecificClass
, bounded byObject
(meaning each type is allowed as a type parameter). Do you really want this?Is the type parameter used inside
Args
? I think the problem would be there.Edit:
The meaning of
is that the caller of the method can decide with which type parameter he wants to call the method, as long as this is some subtype of
AnotherClass
. This means that in effect the method can be called with any objects of typeAnotherClass
.Since the caller can decide the type parameter, you can't in a subclass narrow down the parameter type to
SpecificClass
- this would not be an implementation of the method, but another method with same name (overloading).Maybe you want something like this:
Now the
getAndParse
method implements the parent class' method.That becomes a nonsense when someone has a reference to type GetAndParse and tries to call the getAndParse method. If Cat and Dog extend AnotherClass. I should expect to be able to call GetAndParse#getAndParse with either a Cat or a Dog. But the implementation has tried to restrict it and make it less compatible!
You are seeing this problem because of the concept called "Erasure" in Java Generics. Java uses "erasure" to support backward compatibility. i.e Java code which did not use generics.
Erasure Procedure:
The compiler will first do a type checking and then it will remove(erase) all the type parameters as much as possible, and also insert TypeCasting where ever necessary.
example:
will become
In class "Implementor.java",
The code
will become
the compiler will see that you have not implemented the abstract method correctly. There is a type mismatch between the abstract method and the implemented method. This is why you are seeing the error.
More details can be found here. http://today.java.net/pub/a/today/2003/12/02/explorations.html
You cannot override to specific type T because there is in fact (at the bytecode level if you wish) only one method getAndParse because of type erasure (see other answer):
For every type of T, the same method is used.
You can overload it (I think):
but this will not a different method from (1) ant it will not be called by generic code:
No, it's not valid. What would happen if someone with a
GetAndParse
reference called it with a different class extendingAnotherClass
?