First I would like to say that my question is not specifically about "verifying that a generic object passed as a parameter is a subclass of an abstract class." , but I could not choose a better title. If you find a better phrase, feel free to change.
Well, my situation is this, I have an interface GerenciarClientes
, which specifies operations to manage clients, an abstract class AbstractCliente
that contains default parameters that every subclass of it must have, and an abstract class AbstractGerenciadorClientes
that implements GerenciarClientes
and has an abstract method:
ManageCustomers :
public interface GerenciarClientes{
void adicionar(Object cliente);
void remover(Object cliente);
void editar(Object cliente);
}
AbstractClient :
public abstract class AbstractCliente{
protected int codigo;
public AbstractClient(int codigo){
this.codigo = codigo;
}
}
AbstractGencer :
public class AbstractGerenciador implements GerenciarClientes{
/* Construtor */
@Override
void adicionar(Object cliente);
@Override
void remover(Object cliente);
@Override
void editar(Object cliente);
protected abstract boolean verificaDadosCliente(Object cliente);
}
My problem is this: When I create the class MeuGerenciador
, which extends AbstractGerenciadorCliente
, I'm forced to implement the verificaDadosCliente(Object cliente)
method and pass an object as a parameter. In this case, I would like the object to be a subclass of AbstractCliente
(suppose the name of the subclass is MeuCliente
). In the class AbstractCliente
I got to change the parameter of the abstract method to Class<? extends AbstractCliente> cliente
, but the problem is that when I do this, I can not perform typeCast for class MeuCliente
protected abstract boolean verificaDadosCliente(Class< ? extends AbstractCliente> cliente){
MeuCliente c = (MeuCliente) cliente; // ERRO
}
Object
cliente
) as I can verify that the object extends
AbstractCliente
?