When do I need to use a class that inherits from an ArrayList of a type? I do not understand what this is for

5

Example:

 public class ListaAdapterItem extends ArrayList<Item>{

 }

And I have an Item class:

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 could someone explain me?

    
asked by anonymous 23.11.2016 / 12:44

2 answers

4

Great, it usually does not have that benefit. It may exist, but it is rare to find such a case.

You have 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 time and he considered that a team is a list of players.

But in fact a team is a team. A team has a list of players. According to the Liskov principle , inheritance should only occur when one type is actually the same as the other type. A team can not 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 you're often told to prefer composition instead of inheritance . Inheritance is overvalued.

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

If it is to use with some legacy code that requires a specialized list and not a generic list, then okay, do what? You are using a bad API, do what you have to do, "hold hands."

In the other answer you talk about using when you really need to extend a list and add a behavior. Okay, that's a valid reason. But it is not usually true. If you need to manipulate something internal in the list and have something protected, it would be useful. But there is nothing protected in ArrayList . What is private can not be manipulated anyway. What is public can be done with a utility method external to the class. Inheritance is usually not necessary just because of this, because you are not expected to use this new method that would be added to any existing APIs. And in new code you are creating you can use the utility method. Do not say never, but in general inheritance does not make sense at this point either.

I even ran a search to see where to use ListAdapter and found that Android uses . But note that they were made right, this type inherits from Adapter , that is, an adapter list is an adapter, not a list. It has nothing to do with what was put in the question.

Your intuition seems to be right. Unless it has a larger context that indicates its use.

    
23.11.2016 / 13:00
1

In case you want to make an arraylist with a proper behavior ... For example, if you use arraylist and imagined a new method that you would need to have existed but does not exist yet, you create a class MyArrayList that inherits from arrayList. This way, you can create any method you need, such as "excludePars" ... When you need it, instantiate this MyArrayList and use the method excludePars.

    
23.11.2016 / 13:14