eredità e di override regole metodo generico Java
-
27-10-2019 - |
Domanda
Ho una classe astratta che ha un metodo generico e voglio ignorare il metodo generico sostituendo tipi specifici per il parametro generico. Quindi, in pseudo-codice che ho il seguente:
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
}
}
Ma per qualche motivo sto non ha permesso di fare questo? Sto facendo un qualche tipo di errore di sintassi o è questo tipo di eredità e l'override non consentito? In particolare sto ricevendo un errore circa @Override
perché l'IDE Eclipse continua a ricordarmi di implementare getAndParse
.
Ecco come voglio che il codice di cui sopra al lavoro. Da qualche altra parte nel mio codice v'è un metodo che si aspetta che le istanze di oggetti che implementano GetAndParse
che specificamente significa che hanno un metodo getAndParse
che posso usare. Quando chiamo getAndParse
in tale istanza i controlli del compilatore per vedere se ho usato istanze specifiche di T
in modo corretto, in modo in particolare T
dovrebbe estendersi AnotherClass
e dovrebbe essere SpecificClass
.
Soluzione
What we are having here is two different methods with individual type parameters each.
public abstract <T extends AnotherClass> void getAndParse(Args... args);
This is a method with a type parameter named T, and bounded by AnotherClass
, meaning each subtype of AnotherClass
is allowed as a type parameter.
public <SpecificClass> void getAndParse(Args... args)
This is a method with a type parameter named SpecificClass
, bounded by Object
(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.
The meaning of
public abstract <T extends AnotherClass> void getAndParse(T... args);
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 type AnotherClass
.
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:
public abstract class GetAndParse<T extends AnotherClass> {
public SomeClass var;
public abstract void getAndParse(T... args);
}
public class Implementor extends GetAndParse<SpecificClass> {
// some field declarations
// some method declarations
@Override
public void getAndParse(SpecificClass... args) {
// method body making use of args
}
}
Now the getAndParse
method implements the parent class' method.
Altri suggerimenti
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:
public abstract <T extends AnotherClass> void getAndParse(T paramAnotherClass);
will become
public abstract void getAndParse(AnotherClass paramAnotherClass);
In class "Implementor.java",
The code
public <SpecificClass> void getAndParse(T paramAnotherClass)
will become
public void getAndParse(SpecificClass paramAnotherClass){ }
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
No, it's not valid. What would happen if someone with a GetAndParse
reference called it with a different class extending AnotherClass
?
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 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):
public abstract void getAndParse(AnotherClass... args); // (1)
For every type of T, the same method is used.
You can overload it (I think):
public void getAndParse(SpecificClass... args); // (2)
but this will not a different method from (1) ant it will not be called by generic code:
T x = whatever;
object.getAndParse(x); // Calls (1) even if T is derived from SpecificClass
Static method can't override
class Vehicle{
static void park(int location){
System.out.println("Vehicle parking..");
}}
class Car extends Vehicle{
@Override //error
void park(int location) { //error
System.out.println("Car Parking..");
}}
Private method can't override
class Vehicle{
private void park(int location){
System.out.println("Vehicle parking..");
}
void callPark(){
park(100);
}}
class Car extends Vehicle{
//@Override
void park(int location) {
System.out.println("Car Parking..");
}}
class Demo {
public static void main(String[] args) {
Vehicle v1=new Car();
v1.callPark();
}}
Final method can't override
class Vehicle{
final void park(int location){
System.out.println("Vehicle parking..");
}}
class Car extends Vehicle{
//@Override
void park(int location) { //error
System.out.println("Car Parking..");
}}