I have a class Avisos
of which two types of ads inherit. I save them all in an ArrayList and I could not access the getters of one of the children ads.
I need to get ads of type AvisosLimpiar
on a certain date and I am using the following code to traverse the ArrayList:
for(Object av : listaDeAvisos){
if( ((AvisosLimpiar) av).getDia() == diaInicial){
System.out.println("Datos del aviso...");
}
}
I could not access the getters of the daughter class until using Netbeans has autocompleted with (AvisosLimpiar) av)
. and I have managed to access, but I do not understand exactly what this code does.
How can I access methods of the daughter classes?
Edit: Code according to solution:
for (Avisos av : listaDeAvisos) {
if (av instanceof AvisosLlamada) {
AvisosLlamada avLLamada = (AvisosLlamada) av;
if ((avLLamada.getDia() >= diaInicial && avLLamada.getHora() >= horaInicial) || (avLLamada.getDia() <= diaFinal && avLLamada.getHora() <= horaFinal)){
System.out.println("Aviso del día " + avLLamada.getDia() + " hora " + avLLamada.getHora());
System.out.println("Habitación: " + avLLamada.getHabitacion());
}
}
}