I have a homework to do and I've got some problems with downcasting. Here are my three classes.
import java.util.LinkedList;
public class Bank {
LinkedList<Client> ListOfClients = new LinkedList<Client>();
public void addClient(Client arg){
ListOfClients.add(arg);
}
public void getClients(){
for(Client c : ListOfClients)
System.out.printf("Name: %s \nSurname: %s \nSocial Security: %s\n", c.name, c.surname, c.SocialSecurity);
}
}
public class Client {
LinkedList<Account> ListOfAccounts = new LinkedList<Account>();
public void addAccount(Account args){
ListOfAccounts.add(args);
}
public void getAccounts(){
for(Account a : ListOfAccounts)
System.out.printf("Number: %s\nBalance: %f\n", a.nr, a.balance);
}
public class Person extends Client{
protected String name, surname, SocialSecurity;
public Person(){
System.out.println("Type name: ");
name = input_data();
System.out.println("Type surname: ");
surname = input_data();
System.out.println("Type Social Security number: ");
SocialSecurity = input_data();
}
public String input_data(){
Scanner input = new Scanner(System.in);
String data = input.nextLine();
return data;
}
}
So the problem is with my getClients method in Bank class. It says: "SocialSecurity cannot be resolved or is not a field". Same with name and surname fields. I know that i can copy those strings into Client class and the problem will be solved, but i need to do that with downcasting. I read something about RTTI, but still cannot find any solution for that problem.