-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathObjectSerializationInInheritance.java
More file actions
63 lines (50 loc) · 1.37 KB
/
Copy pathObjectSerializationInInheritance.java
File metadata and controls
63 lines (50 loc) · 1.37 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package inheritance;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* Java Program to demonstrate that if supper class is
* serializable then subclass is automatically
* serializable
*
*/
class A implements Serializable {
int i;
public A(int i) {
this.i = i;
}
}
class B extends A {
int j;
public B(int i, int j) {
super(i);
this.j = j;
}
}
public class ObjectSerializationInInheritance {
public static void main(String[] args) throws Exception {
B b1 = new B(10, 20);
System.out.println("i = " + b1.i);
System.out.println("j = " + b1.j);
//Saving of object in a file
FileOutputStream fos = new FileOutputStream("file.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// Method for serialization of B's class object
oos.writeObject(b1);
oos.close();
fos.close();
System.out.println("Object has been serialized");
//Reading the object from a file
FileInputStream fis = new FileInputStream("file.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
// Method for de-serialization of B's class object
B b2 = (B)ois.readObject();
ois.close();
fis.close();
System.out.println("Object has been deserialized");
System.out.println("i = " + b2.i);
System.out.println("J = "+b2.j);
}
}