Check out example codes for "Write a Java program to store the objects of the class Employee (Emp_id, Name, Designation and salary) in a file in sequential order. De-serialize the same file to display the details of the employees whose salary is lesser than 50000.". It will help you in understanding the concepts better.

Code Example 1

1  package serialClone;
 2  
 3  /**
 4  * @version 1.22 2018-05-01
 5  * @author Cay Horstmann
 6  */
 7  
 8  import java.io.*;
 9  import java.time.*;
10  
11  public class SerialCloneTest
12  {
13    public static void main(String[] args) throws CloneNotSupportedException
14    {
15        var harry = new Employee("Harry Hacker", 35000, 1989, 10, 1);
16        // clone harry
17        var harry2 = (Employee) harry.clone();
18  
19        // mutate harry
20        harry.raiseSalary(10);
21  
22        // now harry and the clone are different
23        System.out.println(harry);
24        System.out.println(harry2);
25      }
26  }
27  
28  /**
29  * A class whose clone method uses serialization.
30  */
31  class SerialCloneable implements Cloneable, Serializable
32  {
33    public Object clone() throws CloneNotSupportedException
34    {
35        try {
36          // save the object to a byte array
37          var bout = new ByteArrayOutputStream();
38          try (var out = new ObjectOutputStream(bout))
39          {
40              out.writeObject(this);
41          }
42    
43          // read a clone of the object from the byte array
44          try (var bin = new ByteArrayInputStream(bout.toByteArray()))
45          {
46              var in = new ObjectInputStream(bin);
47              return in.readObject();
48          }
49        }
50        catch (IOException | ClassNotFoundException e)
51        {
52          var e2 = new CloneNotSupportedException();
53          e2.initCause(e);
54          throw e2;
55        }
56      }
57  }
58  
59  /**
60  * The familiar Employee class, redefined to extend the
61  * SerialCloneable class.
62  */
63  class Employee extends SerialCloneable
64  {
65      private String name;
66      private double salary;
67      private LocalDate hireDay;
68  
69      public Employee(String n, double s, int year, int month, int day)
70      {
71        name = n;
72        salary = s;
73        hireDay = LocalDate.of(year, month, day);
74      }
75  
76      public String getName()
77      {
78          return name;
79      }
80  
81      public double getSalary()
82      {
83          return salary;
84      }
85  
86      public LocalDate getHireDay()
87      {
88          return hireDay;
89      }
90  
91      /**
92          Raises the salary of this employee.
93          @byPercent the percentage of the raise
94      */
95      public void raiseSalary(double byPercent)
96      {
97          double raise = salary * byPercent / 100;
98          salary += raise;
99      }
100 
101      public String toString()
102      {
103        return getClass().getName()
104            + "[name=" + name
105            + ",salary=" + salary
106            + ",hireDay=" + hireDay
107            + "]";
108      }
109  }

Learn ReactJs, React Native from akashmittal.com