Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override method with different signature

I have a superclass with the 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 its 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 this just doesn't work. The problem is that I have a reference to a superclass, and I want to call this method, but only in one of the subclasses.

like image 286
danillosl Avatar asked Sep 12 '13 20:09

danillosl


People also ask

Can we change signature of overridden method?

No, while overriding a method of the super class we need to make sure that both methods have same name, same parameters and, same return type else they both will be treated as different methods.

Does overriding methods have same method signature?

Overriding occurs when the method signature is the same in the superclass and the child class. Overloading occurs when two or more methods in the same class have the same name but different parameters.

Can we override same method with different return type?

Java version 5.0 onwards it is possible to have different return types for an overriding method in the child class, but the child's return type should be a subtype of the parent's return type. The overriding method becomes variant with respect to return type.

Can we override methods with same signature but in child class it is throwing exception?

Overridden methods can throw Exceptions, so long as the method being overridden also throws the same Exceptions. You can't introduce new Exceptions.


1 Answers

According to java overridding

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

Here your method return type is different so it is not overridding.

like image 197
Prabhaker A Avatar answered Sep 18 '22 08:09

Prabhaker A