Dario Birtone
Dario Birtone

Reputation: 35

acces to a subclass variable in arrayList

I want to access the method getDimensioni of an element of a subclass of Campo called Calcio.

Here's the code where I want to do it:

ArrayList<Campo> campiprenotati = new ArrayList<>();

public void prenotazioniCC7(){
    for (Calcio calcio : campiprenotati) {
        if("7".equals(calcio.getDimensioni())) { 
            System.out.println(calcio.tostring());
        } else {
            System.out.println("Campo non trovato");
        }
        }
    }
}

Here's Calcio.java:

public class Calcio extends Campo {
    private String dimensioni;

    public Calcio(String ID, boolean illuminazione, String dimensioni) {
        super(ID, illuminazione);
        this.dimensioni = dimensioni;
    }

    public void setDimensioni() {
        this.dimensioni = dimensioni;
    }

    public String getDimensioni() {
        return dimensioni;
    }

    @Override public String toString() {
        return super.toString() + " " + dimensioni;
    }
}

Upvotes: 1

Views: 174

Answers (2)

Nishant Raj
Nishant Raj

Reputation: 76

it depends how are calling in your use case and where you are creating this list, inject your list object in your base class using constructor and use in methods, if your list is creating at run time you can inject this list in constructor.

ArrayList<Campo> campiprenotati;

public MyClass(final ArrayList<Campo> campiprenotati){
    this.campiprenotati = campiprenotati;
}

Now you can create and pass list object of subclass and use it, but still it is not good design of your code.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074335

Since your ArrayList is of Campo elements, you can't know that only contains Calcio elements (it may have elements of type Campo or other subclasses), so you can't use Calcio for your for loop element type. You could use Campo and then use instanceof to find out if the Campo is a Calcio, like this:

public void prenotazioniCC7(){
    for (Campo campo : campiprenotati) {
// ------^^^^^^^^^^^
        if (campo instanceof Calcio && "7".equals(((Calcio)campo).getDimensioni())) { 
// ---------^^^^^^^^^^^^^^^^^^^^^^^^^^------------^^^^^^^^^^^^^^^
            System.out.println(campo.toString());
// ----------------------------^^^^^
        } else {
            System.out.println("Campo non trovato");
        }
    }
}

but using instanceof generally (though not always) suggests that you may want to structure things differently, for instance by making the array only contain Calcio elements.

For instance, oOne approach (as suggested by TungstenX in the comments), would be to identify all the common aspects of the elements that need to be in this list (and lists like it) and put them in an interface that all of the relevant classes implement:

public class Campo /*...*/

public interface TheInterface {
    String getDimensioni();
}

public class Calcio extends Campo implements TheInterface /*...*/
//                  ^^^^^^^^^^^^^-- only if necessary and appropriate

then

ArrayList<TheInterface> campiprenotati = new ArrayList<>();

public void prenotazioniCC7(){
    for (TheInterface entry : campiprenotati) {
        if ("7".equals(entry.getDimensioni())) { 
            System.out.println(entry.toString());
        } else {
            System.out.println("Campo non trovato");
        }
    }
}

Upvotes: 4

Related Questions