This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Previously we have discussed the Program for
Java Program for finding duplicate element from ArrayList ?
Java Program for finding duplicate element from ArrayList ?
Implementing Serializable interface in super class make all sub class Serializable by default.
Static data member are not serialized because static is the part of class not object.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class X implements Serializable{
}
class Y extends X {
}
class Z extends Y {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class SeriazableExample extends Z{
String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public static void main(String[] args) throws FileNotFoundException, IOException,ClassNotFoundException {
Z z= new Z();
z.setName("Pushkar Khosla");
SeriazableExample t = new SeriazableExample();
t.setAddress("Uttam Nagar");
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("D:/zzz.txt")));
out.writeObject(z);
out.writeObject(t);
out.flush();
out.close();
System.out.println("Reading Data From File In Form Of Object : ");
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("D:/zzz.txt")));
Z z1 = (Z)in.readObject();
System.out.println("NAME : "+z1.getName());
SeriazableExample t1 =(SeriazableExample)in.readObject();
System.out.println("ADDRESS : "+t1.getAddress());
}
}
Program Output :-

No comments:
Post a Comment