When do I need to use a class that inherits from an Arraylist of a kind? I don’t understand what that’s for

Asked

Viewed 134 times

5

Example:

 public class ListaAdapterItem extends ArrayList<Item>{

 }

And I got a class Item:

public class Item {
private int imagem;
private String nome;
private String descricao;

public Item(int imagem, String nome, String descricao){
    this.imagem = imagem;
    this.nome = nome;
    this.descricao = descricao;
}

public int getImagem() {
    return imagem;
}
public void setImagem(int imagem) {
    this.imagem = imagem;
}
public String getNome() {
    return nome;
}
public void setNome(String nome) {
    this.nome = nome;
}
public String getDescricao() {
    return descricao;
}
public void setDescricao(String descricao) {
    this.descricao = descricao;
}

}

And in the Main class to test this code would look something like:

    public static void main(String[] args) {

    Item item = new Item(23, "nome", "descricao");

    ListaAdapterItem itens = new ListaAdapterItem();
    itens.add(item);

    }

Seriously I can not understand any benefit in this use someone could explain me?

2 answers

5


Great, in general it does not have this benefit even. It may exist, but it is rare to find a case like this.

Has a famous question in Eric Lippert’s OS that talks about it. It shows that the AP wanted to create a list that is a team and he considered that a team is a list of players.

But actually a team is a team. A team possesses a list of players. According to the liskov principle, inheritance should only occur when actually one type is the same as the other type. A team cannot inherit from a list, a team is not a list, but it is composed of a list of players, possibly other properties as well.

That’s why they say to prefer composition in place of inheritance. Heritage is overrated.

I do not know the context of this case, the question does not speak well what this is ListAdapter, if only to create a specialized list of Item, must be a mistake.

If it is to use with some legacy code that requires a specialized list and not a generic list, then all right, started wrong, do what? You’re using a bad API, do what you have to do, "Get your hands dirty".

In the other answer it talks about using when you really need to extend a list and add a behavior. Ok, that’s a reason that would be valid. But it’s not usually real. If you need to manipulate something inside the list that has something protected, it would be useful. But there’s nothing protected in ArrayList. What is private cannot be manipulated anyway. What is public can be done with a utility method outside the class. In general inheritance is not only necessary because of this, because it is not expected to use this new method that would be added in any existing API. And in new code you are creating you can use the utility method. Don’t say never, but in general inheritance also makes no sense at this point.

I even did a little research to see where to use one ListAdapter and found that Android uses. But note that they did right, this kind inherits from Adapter, that is, an adaptor list is an adaptor, not a list. It has nothing to do with what was asked in the question.

Your intuition seems to be right. Unless you have a larger context that indicates your use.

  • Massa, I learned real young.

1

Serves in case you want to make an arraylist with a behavior of its own... For example, if Voce uses arraylist and imagined a new method that Voce would need to exist but doesn’t exist yet, you create a Meuarraylist class that you inherit from arrayList. This way, you can create any method that you need, such as "delete"... When you need it, run this Meuarraylist and use the delete menu.

Browser other questions tagged

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