object stream
object stream
1. Serialization:
o The process of converting an object's state (its fields and properties) into a format
that can be stored or transmitted.
o This format is typically a binary or text-based representation of the object.
2. Deserialization:
o The reverse of serialization, where the serialized data is converted back into an
object with its original state.
3. Stream:
o A stream is an abstraction that represents a sequence of data. In the context of
object streams, this data represents the serialized form of objects.
1. Data Persistence:
2. Network Communication:
3. Deep Cloning:
Create an exact copy of an object, including nested objects, by serializing and deserializing.
Useful for scenarios where a complete duplication of state is required.
4. Caching:
Java provides built-in support for object streams through the java.io package.
Classes:
o ObjectOutputStream: Writes objects to an output stream.
o ObjectInputStream: Reads objects from an input stream.
Serializable Interface:
o Objects need to implement the Serializable interface to be serialized.
o Example:
java
CopyEdit
import java.io.*;
// Deserialization
FileInputStream fileIn = new
FileInputStream("objectData.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
@Override
public String toString() {
return "MyObject{message='" + message + "', value=" +
value + '}';
}
}
Limitations
Object streams are a powerful feature of OOP, enabling efficient and flexible handling of objects
across different mediums.
Serialization in OOP
1. Serialization in OOP
Serialization is the process of converting an object (an instance of a class) into a byte stream.
This allows the object's state (its fields or attributes) to be saved or transmitted.
OOP Context:
Objects as Data Units: Objects are instances of classes that contain both data (attributes)
and behavior (methods). Serialization focuses on persisting the state (attribute values) of
an object.
Class Responsibility: A class that supports serialization must explicitly declare that it
can be serialized by implementing the Serializable interface. This is part of the
contract that allows other parts of the system to know the object can be saved/transmitted.
Example:
A Person class with attributes like name, age, and password. Serialization saves the state of a
Person object to a file or sends it across a network.
2. Deserialization in OOP
Deserialization is the reverse of serialization. It reconstructs an object by reading its byte stream
and restoring its state.
OOP Context:
Object Lifecycle: Deserialization creates a new object from the saved byte stream,
restoring its state as it was during serialization.
Encapsulation: Only the object's state is saved and restored. Private fields are preserved,
maintaining encapsulation.
Reusability: Deserialization allows objects to persist beyond a single runtime session or
process.
Example:
The Person object serialized earlier can be deserialized to create a new Person instance with the
same state (values of name and age).
Encapsulation:
Only the object's state is serialized, and private/protected fields are handled properly.
Fields can be excluded using the transient keyword to protect sensitive data.
Inheritance:
If a class implements Serializable, all its subclasses are also serializable unless
explicitly stated otherwise.
Serialization respects the inheritance chain: all parent class fields (if serializable) are
included.
Polymorphism:
Abstraction:
OOP Example
Class Definition
java
CopyEdit
import java.io.*;
// Constructor
public Person(String name, int age, String password) {
this.name = name;
this.age = age;
this.password = password;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + ", password='" +
password + "'}";
}
}
java
CopyEdit
public class SerializationExample {
public static void main(String[] args) {
String fileName = "person.ser";
// Deserialize Object
try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream(fileName))) {
Person deserializedPerson = (Person) ois.readObject(); //
Deserialize the object
System.out.println("Deserialized: " + deserializedPerson);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
1. Encapsulation:
o Fields like name and age are serialized and restored. The password field is
excluded because it’s marked transient.
2. Polymorphism and Reusability:
o The Person object can be reused across different processes by serializing and
deserializing its state.
3. Abstraction:
o The ObjectOutputStream and ObjectInputStream abstract the byte stream
handling.
4. Inheritance:
o If a subclass of Person were serialized, its additional fields would also be
included (as long as they are serializable).
1. Object Persistence:
o Serialization allows objects to be saved to a file or database and restored later.
This makes it easy to store the state of objects and resume operations after a
system restart.
2. Data Transmission:
o Serialized objects can be sent over a network, enabling communication between
systems in distributed applications (e.g., Remote Method Invocation (RMI), web
services).
3. Encapsulation Preserved:
o Serialization handles private and protected fields, maintaining encapsulation.
Sensitive fields can be excluded using the transient keyword.
4. Polymorphism Supported:
o Serialized objects retain their actual class type, even if deserialized into a parent
class reference. This ensures the behavior and identity of objects are preserved.
5. Abstraction of Complex Data:
o Serialization abstracts the process of converting objects into a byte stream,
simplifying the storage and transfer of complex data structures.
6. Deep Copy Implementation:
o Serialization and deserialization can be used to create deep copies of objects,
which is particularly useful when objects contain references to other objects.