I have to use Polymorphism and ArrayList in my assignment in java but i need help. I would really appreciate it if anyone could help me. I can't acces my subclass methods from ArrayLis.
So I have a superclass
public class Table{
private int TotalAmount;
//setters and getters
}
And I have two subclasses
public class TableFidelity extends Table{
private int Points;
//setters and getters
}
public class TableCompany extends Table{
private int NumberOfTimesServed;
//setters and getters
}
I have to use both my subclasses in the same ArrayList
public static void main(String[] args){
Table table=new Table();
ArrayList<Table> Tables=new ArrayList<Table>();
Table tablefidelity =new TableFidelity ();
Table tablecompany =new TableCompany ();
Tables.add(tablefidelity);
Tables.add(tablecompany);
System.out.println(Tables.get(0).getTotalAmount());
//I can get the total Amount from the superclass
System.out.println(Tables.get(0).getPoints());
//but I can't do this
System.out.println(Tables.get(1).NumberOfTimesServed());
//or this
}
Netbeans dosen't show getPoints or NumberOfTimesServed when I press . it shows only the superclass methods.
Table; it can't know that there might be specific subclasses there, or that any particular element will be an element of a particular subclass.Table. It doesn't know that element 0 is aTableFidelityetc. (and it shouldn't know that).((TableFidelity)(Tables.get(0)).getPoints())