Java Program on Serialization with Inheritance
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Program 1
package dataflair;
public class Department extends Employee
{
private int salary;
private String deptname;
public Department() {
}
public Department(int eid, String ename, int mobile,int salary,String deptname)
{
super(eid,ename,mobile);
this.salary=salary;
this.deptname=deptname;
}
public String toString()
{
return "Emp Id=" +eid + " "+"Emp Name=" +ename + " " +"Mobile=" + mobile + " " + "Salary=" +salary + " " +"Department=" + deptname;
}
}Program 2
package dataflair;
import java.io.Serializable;
public class Employee implements Serializable
{
int eid;
String ename;
int mobile;
public Employee()
{
}
public Employee(int eid, String ename, int mobile) {
this.eid = eid;
this.ename = ename;
this.mobile = mobile;
}
}Program 3
package dataflair;
import java.io.*;
public class TestEmpRead
{
public static void main(String[] args) throws IOException,ClassNotFoundException
{
File F=new File("d://dataflair/empdept.txt");
if(F.isFile())
{
FileInputStream fis=null;
ObjectInputStream ois=null;
try
{
fis=new FileInputStream(F);
ois=new ObjectInputStream(fis);
Department D1;
D1=(Department)(ois.readObject());
System.out.println(D1);
}
finally
{
fis.close();
ois.close();
}
}
else
System.out.println("File not found.........");
}
}program 4
package dataflair;
import java.io.*;
public class TestEmpwrite
{
public static void main(String[] args) throws IOException
{
File F=new File("d://dataflair/empdept.txt");
FileOutputStream fos=null;
ObjectOutputStream oos=null;
try
{
fos=new FileOutputStream(F);
oos=new ObjectOutputStream(fos);
Department D=new Department(101, "Ashish Verma",112233, 56000, "IT");
oos.writeObject(D);
System.out.println("Record Save............");
}
finally
{
fos.close();
oos.close();
}
}
}
Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

