How to get the objects from superclass to subclass in java? -
a class person has several subclasses including employee.
there method supposed return list consists of employee objects original list of person.
how accomplish in java?
simple example you:
public class test { public static void main(string[] args) { list<person> lst = new arraylist<person>() { { add(new person()); add(new person()); add(new employee()); add(new employee()); add(new anotherperson()); add(new anotherperson()); } }; list<person> employes = lst.stream().filter(item -> item instanceof employee).collect(collectors.tolist()); system.out.println(employes.size()); } class person { } class employee extends person { } class anotherperson extends person { } }
Comments
Post a Comment