Get a class that has a certain interface implemented in generics [closed]

0

Hello.

I want to do something like metodo(Class<? extends ClasseAbstrata> classe) , except that an abstract class, I want to use a interface , but if I change from extends to implements eclipse starts to point an error. >

What do I do?

Another question is that if I change the abstract class by a interface , and I leave extends ( Class<? extends Interface> ), eclipse does not point to error but does not: public class Classe extends Interface { ... .

Is this an eclipse error? Or is there some kind of class that can be done like this?

I have three classes that implement interface ( Acao ), and I created another interface ( AcaoController ), and this interface has method public void inicializaEdicao(Class<?> classe); and I want it to receive all methods which implement Acao .

Interface:

public interface Acao {

    public String dadosParaGravacao();

    public Person criaPerson();

}

Class Compromisso :

public class Compromisso implements Acao { 

    ...

    @Override
    public String dadosParaGravacao() {

        return new StringBuilder().append(descricao)
                .append("|").append(titulo)
                .append("|").append(importancia)
                .append("|").append(feito)
                .append("|").append(atrazado)
                .append("|").append(horario.toString())
                .append("|").append(dataDeCriacao.toString())
                .append("|").append(dataDoFinal.toString())
                .append("|").toString();

    }

    @Override
    public Person criaPerson() {

        return new CompromissoPerson(titulo, importancia, feito);

    }
}

Class Projeto :

public class Projeto implements Acao { 

    ...

    @Override
    public String dadosParaGravacao() {

        return new StringBuilder().append(descricao)
            .append("|").append(titulo)
            .append("|").append(importancia)
            .append("|").append(feito)
            .append("|").append(atrazado)
            .append("|").append(dataDeCriacao.toString())
            .append("|").append(dataDoFinal.toString())
            .append("|").toString();

    }

    @Override
    public Person criaPerson() {

        return new ProjetoPerson(titulo, importancia, feito);

    }
}

Class Favorito :

public class Favorito implements Acao { 

    ...

    @Override
    public String dadosParaGravacao() {

        return new StringBuilder().append(descricao)
            .append("|").append(titulo)
            .append("|").append(importancia)
            .append("|").append(link)
            .append("|").append(dataDeCriacao.toString())
            .append("|").toString();

    }

    @Override
    public Person criaPerson() {

        return new FavoritoPerson(titulo, importancia, dataDeCriacao.toString());

    }
}

Interface AcaoController :

public interface AcaoController {

    public List<Person> novo();

    public void inicializaEdicao(Class<?> classe);

}

The error continues:

    
asked by anonymous 24.06.2016 / 23:51

1 answer

3

In generics, you always use the keywords extends or super . It does not matter if it is a class that implements an interface or that extends another class. These are totally different concepts, they just reused existing keywords in the language so that new keywords did not have to be introduced.

So if you have:

public interface Y {}
public class X implements Y {}

You declare:

// Aceita qualquer classe que implemente (com implements) a interface X;
// Qualquer subinterface que herde (com extends) a interface X;
// Ou aceita a própria interface Y (Y.class).
void metodo(Class<? extends Y> classe);

The same as if you have:

public abstract class B {}
public class A extends B {}

You declare:

// Aceita qualquer subclasse (com extends) da classe B;
// Ou aceita a própria classe B (B.class).
void metodo(Class<? extends B> classe);

In generics, it does not matter if it was extends or implements , the keyword to be used will always be extends .

There is the case of super as well. If you have:

public interface K {}
public interface L {}
public interface H extends S {}
public interface S {}
public class G extends J implements K, L {}
public class F extends G implements H {}
public class M extends F {}
public class R {}
public interface Q extends H {}

So with that:

// Aceita qualquer superclasse ou superinterface de F, além da própria classe F (F.class).
// Aceita F.class, G.class, H.class, J.class, K.class, L.class, S.class ou Object.class.
// Não aceita M.class, R.class ou Q.class.
void metodo(Class<? super F> classe);

Because while extends "looks down" (ie for more specific types), super "looks up" (ie for more generalized types).

So this is your method:

public void inicializaEdicao(Class<?> classe);

It will look like this:

public void inicializaEdicao(Class<? extends Acao> classe);
    
25.06.2016 / 00:14