Skip to content

Instantly share code, notes, and snippets.

@Maslor
Last active August 29, 2015 14:25
Show Gist options
  • Save Maslor/19853de3d7e18f17dfb2 to your computer and use it in GitHub Desktop.
Save Maslor/19853de3d7e18f17dfb2 to your computer and use it in GitHub Desktop.
Getting a subclass element from the abstract superclass type list
private List<Manager> _managers = new ArrayList<Manager>();
private List<Employee> _employees = new ArrayList<Employee>();
private List<User> _usrs = new ArrayList<User>();
public User findUser(String userID){
refreshUserList(); //updates _usrs so it has all the current users
Iterator<User> i;
User u = null; //abstract class User
i = this._usrs.iterator();
while(i.hasNext()) {
u = i.next();
if(u.getID().equals(userID)){
if(u.getRole().equals("employee")) {
u = (Employee) u; //Employee extends User
break;
}
u = (Manager) u; //Manager extends User
}
}
return u;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment