Java – override method with different signatures
I have a superclass that uses this method:
protected <E extends Enum<E>,T extends VO> void processarRelatorioComEstado(Date dataInicial,Date dataFinal,E estado) throws RelatorioException { throw new UnsupportedOperationException("method not overridden"); }
In one of the subclasses, I want to do the following:
@Override protected <E extends Enum<E>> DemonstrativoReceitaDespesasAnexo12Vo processarRelatorioComEstado(Date dataInicial,E estado) throws RelatorioException { //do something return DemonstrativoReceitaDespesasAnexo12Vo; }
But it just doesn't work The problem is that I have a superclass reference. I want to call this method, but I can only call it in one of the subclasses.
Solution
You cannot change the number of type parameters in an override method As for your case, overwrite obviously failed, return type But even if the return type is the same, your method will not override the equivalent because you have fewer type parameters in the so-called overriding method
Start with JLS – method signature:
Therefore, even the following code will fail:
interface Demo { public <S,T> void show(); } class DemoImpl implements Demo { @Override public <T> void show() { } // Compiler error }
Because there are few type parameters, the method show () in the class will not be equivalent to the method in the interface
Therefore, you should ensure that the method signature is exactly the same as that specified in the JLS section (the same name, the same number and type of parameters (including type parameters), covariate return type)