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

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -