Take a class that has a certain interface implemented in Generics

Asked

Viewed 382 times

0

Hello.

I want to do something like metodo(Class<? extends ClasseAbstrata> classe), only that of an abstract class, I want to use a interface, but I’ve moved on from extends for implements the eclipse starts pointing an error.

What I do?

Another question is that you trade the abstract class for one interface, and leave extends (Class<? extends Interface>), the eclipse does not point error, but does not make: public class Classe extends Interface { ....

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


I have three classes that implement the interface (Acao), And created another interface (AcaoController), and that interface has the method public void inicializaEdicao(Class<?> classe); and I want him to receive all the methods they 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:

inserir a descrição da imagem aqui

  • 1

    Because not only public void inicializaEdicao(Acao a);? What your controller does that it needs the class and not the instance?

  • I didn’t realize that. Thank you.

  • Then change the parameter to the type Acao resolved?

  • There was no problem. So yes.

1 answer

3

In Generics, you always use the keywords extends or super. It doesn’t matter if it’s a class that implements an interface or what extends another class. They 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 way 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 the generics, it doesn’t matter if it was extends or implements, the keyword to be used will always be extends.

There is the case of super too. 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 as the extends "looks down" (ie for more specific types), the super "looks up" (i.e., to more widespread types).


So this is your method:

public void inicializaEdicao(Class<?> classe);

Will stay like this:

public void inicializaEdicao(Class<? extends Acao> classe);
  • I edited the question.

  • @Vitor Stafusa great answer!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.