title: How to add different subclass object types from text file to an array list of type parent class?
body: <p>I was given an assignment to create a Employee parent class with several extended classes (manager, salaried employee etc.) I have completed the assignment but, I wanted to see if I could go a step further and attempt to read and add objects to a text file with different object types so that I can save objects created from the last time the program ran. This is proving a bit more tricky than reading objects of the same type from a text file since the manager, salaried employee and hourly employee classes that extend from the employee parent class have different constructor parameters. I have done a bit of research and came across serialization but am still a tad confused.</p>
  
  <p>Current constructors are:
  - Employee (String name,String title, int id)
  - Manager(String name,String title, int id,double salary, double bonus)
  - SalariedEmployee(String name,String title, int id,double salary)
  - HourlyEmployee(String name,String title, int id,double wage, double<br>
    hours)</p>
  
  <p>A sample of the text file I'd like to read and initialize an ArrayList from:</p>
  
  <p>Morgan Lee; Manager; 109; 120000; 20 </p>
  
  <p>Harry N; Salaried Employee; 110; 89000</p>
  
  <p>Lee Ann M; Hourly Employee; 111; 18; 40</p>
  
  <p>Recap: I'd like to read and add different object types from a text file into an ArrayList. I've managed to write them to the text file but i really need help reading them!</p>
  
  <p>ps. When i go to check the text file I wrote initialized objects i see this: "��srjava.lang.Integer⠤���8Ivaluexrjava.lang.Number������xpsq~sq~"</p>
  
  <p>I have used PrintWriter and File to read and write single object types to and from a text file by parsing the information and placing it to a String array per line in the past. I now have used serialization implementation only to realize I can't read or add objects actually in the text file.</p>
  
  <pre><code>public static void main(String[] args) throws FileNotFoundException
     {       
         Scanner in = new Scanner (System.in);
         ArrayList&lt;Employee&gt; list = new ArrayList&lt;Employee&gt;();
         String name, title;
         int id, choice, choice2 = 0;
         double wage=0, hours=0, salary=0,bonus=0;
         boolean another = false;
         boolean found = false;
         boolean flag = false;
  
         list.add(new Manager("Morgan Lin", "Manager",105,120000,20));
         list.add(new Manager("Al Mar", "Manager",102,220000,20));
         list.add(new SalariedEmployee("Harry Morgan", "Salaried Employee",101,89000));
  
         try {
             FileOutputStream fo = new FileOutputStream (new File ("myObjects.txt"));
             ObjectOutputStream o = new ObjectOutputStream(fo);
             for(int i = 0; i&lt;list.size();i++) {
             o.writeObject(i);
             }
             o.close();
             fo.close();
  
  
         } catch (FileNotFoundException e) {
              System.out.println("File not found");
          } catch (IOException e) {
              System.out.println("Error initializing stream");
          }
  </code></pre>
  
  <p>I'd like to have something like this in myObjects.txt:</p>
  
  <p>Morgan Lin; Manager; 105; 120000; 20 </p>
  
  <p>Al Mar; Manager; 102; 220000;20
  Harry Morgan; Salaried Employee;101;89000</p>
  
  <p>Without having to first initialize the objects to the ArrayList in my code.</p>