-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderOfSerializationDemo2.java
More file actions
42 lines (36 loc) · 1018 Bytes
/
Copy pathOrderOfSerializationDemo2.java
File metadata and controls
42 lines (36 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// If we don't know the order of serialization.Then this is how we can deserialize the object.
import java.io.*;
class Dog implements Serializable{
Cat c = new Cat();
}
class Cat implements Serializable{
Rat r =new Rat();
//Perform cat related functionality
}
class Rat implements Serializable{
int j=10;
// Perform rat related functionality
}
class OrderOfSerializationDemo2{
public static void main(String[] args){
Dog d1 =new Dog();
FileOutputStream fos =new FileOutputStream("ccc.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d1);
FileInputStream fis = new FileInputStream("ccc.ser");
ObjectInputStream ois =new ObjectInputStream(fis);
Object o = ois.readObject();
// THIS IS HOW YOU CAN DO .NOT COMPLETE CODE
if(o instanceof Dog){
Dog d2 =(Dog)o;
//Perform dog related functionality
}
else if(o instanceof Cat){
Cat c2 =(Cat)o;
}
else if(o instanceof Rat){
Rat r2 = (Rat)o;
}
System.out.println();
}
}