Override Method with a Different Signature

Well, I have a superclass using a 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"); } 

and in one of the subclasses I want to do the following:

  @Override protected <E extends Enum<E>> DemonstrativoReceitaDespesasAnexo12Vo processarRelatorioComEstado(Date dataInicial, Date dataFinal, E estado) throws RelatorioException { //do something return DemonstrativoReceitaDespesasAnexo12Vo; } 

but it just doesn’t work, the problem is that I have a reference to the superclass, and I want to call this method, but only in one of the subclasses.

Sorry for my English, thanks for your patience.

+4
source share
3 answers

You cannot change the number of type parameters in an overridden method. For your case, overriding is clearly not the return type. But even if the return types were the same, your method will still not override the equivalent, since you have fewer type parameters in the intended overridden method.

From JLS - Method Signature :

Two methods have the same signature if they have the same name and argument types.

Two declarations of a method or constructor, M and N, have the same argument if all of the following conditions are true:

  • They have the same number of formal parameters (possibly zero)
  • They have the same number of type parameters (possibly zero)

Thus, even the following code will not work:

 interface Demo { public <S, T> void show(); } class DemoImpl implements Demo { @Override public <T> void show() { } // Compiler error } 

Because the show() method in the class does not override the equivalent using the method in the interface, due to fewer type parameters.

So, you have to make sure that the method signature is exactly the same as indicated in this JLS section (same name, same number and type of parameters (including type parameters), type of the returned sharing option).

+3
source

According to java overridding

An override method has the same name, number, and type of parameters, and the type of the return value is the same as the method that it overrides. The overriding method can also return a subtype of the type returned by the overridden method. This is called the covariant return type.

Here, your return type is different, so it does not overload.

+5
source

Considering the comments above, I understand that this approach will not work, so I make some changes to the code and work like a charm, following the code:

superclass:

 protected VO processarRelatorioComEstado(Date dataInicial, Date dataFinal, Enum<?> estado) throws RelatorioException { throw new UnsupportedOperationException("method not overridden"); } 

and subclass:

 public VO processarRelatorioComEstado(Date dataInicial, Date dataFinal, Enum<?> estado) throws RelatorioException { //do something return VOsubtype; } 

Thanks guys.

+1
source

Source: https://habr.com/ru/post/1501937/


All Articles