codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🧩

OOP Concepts

23 chapters

1Procedural vs Object-Oriented2Classes, Objects, & Instantiation3Constructors & Destructors4Static Members & Methods5Encapsulation & Access Modifiers6Data Abstraction7Inheritance Types (Single, Multiple)8Compile-Time Polymorphism (Overloading)9Polymorphism & Interfaces10Run-Time Polymorphism (Overriding)11Virtual Functions & V-Tables12Interfaces & Abstract Classes13Generic Programming (Templates & Generics)14Exception Handling in OOP15SOLID Design Principles16Composition over Inheritance17Coupling & Cohesion18UML Diagrams Basics19Creational Patterns (Singleton, Factory)20Structural Patterns (Adapter, Decorator)21Behavioral Patterns (Observer, Strategy)22MVC Architecture Pattern23Object Serialization & Cloning
SubjectsOOP Concepts

Object Serialization & Cloning

Updated 2026-04-22
2 min read

Object Serialization & Cloning

1. Serialization

Serialization is the process of converting an object's state into a byte stream so it can be saved to a file, stored in a database, or transmitted over a network. Deserialization is the reverse: reconstructing the object from the byte stream.

Java Serialization:

class Employee implements Serializable {
    private static final long serialVersionUID = 1L;
    String name;
    int age;
    transient String password; // 'transient' fields are NOT serialized
}

// Serialize
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("emp.dat"));
out.writeObject(employee);
out.close();

// Deserialize
ObjectInputStream in = new ObjectInputStream(new FileInputStream("emp.dat"));
Employee emp = (Employee) in.readObject();
in.close();

Modern Serialization Formats:

  • JSON: Human-readable, language-agnostic. Most common for web APIs. Libraries: Jackson, Gson (Java), json (Python).
  • Protocol Buffers (Protobuf): Google's binary serialization format. Extremely compact and fast. Used in gRPC.
  • XML: Verbose but widely supported. Used in SOAP web services and configuration files.

2. Object Cloning

Cloning creates an exact copy of an existing object. There are two types:

Shallow Copy

Copies the object's field values directly. If a field is a reference to another object, only the reference is copied (both the original and the clone point to the SAME inner object).

  • Modifying the inner object through the clone affects the original too.

Deep Copy

Recursively copies all objects referenced by the original. The clone is completely independent.

// Shallow clone in Java
class Student implements Cloneable {
    String name;
    Address address; // Reference type
    
    protected Object clone() throws CloneNotSupportedException {
        return super.clone(); // Shallow copy
    }
}

// Deep clone
protected Object clone() throws CloneNotSupportedException {
    Student cloned = (Student) super.clone();
    cloned.address = new Address(this.address); // Deep copy the Address
    return cloned;
}

3. Copy Constructor

An alternative to cloning. A constructor that takes an object of the same class and creates a new object with the same values. More explicit and less error-prone than clone().

class Student {
    String name;
    Address address;
    
    // Copy Constructor
    Student(Student other) {
        this.name = other.name;
        this.address = new Address(other.address); // Deep copy
    }
}

Many modern style guides prefer Copy Constructors or static factory methods over Java's Cloneable interface, which is considered a broken API by Joshua Bloch in "Effective Java."



PreviousMVC Architecture Pattern

Recommended Gear

MVC Architecture Pattern