Showing posts with label Work. Show all posts
Showing posts with label Work. Show all posts

Tuesday, March 3, 2026

What is Cloning in Java? How Does Cloneable Work?

Cloning is an important concept in Core Java, especially when working with object copying, memory management, and real-time applications.

It is also a commonly asked Java interview question.

Let’s understand it clearly.





🔹 What is Cloning in Java?

Cloning is the process of creating an exact copy of an existing object.

👉 In simple words:
Cloning = Creating a duplicate object with the same state.

Java provides cloning support through:

  • Object class method → clone()

  • Marker interface → Cloneable


🔹 What is Cloneable Interface?

Cloneable is a marker interface present in:

java.lang.Cloneable

It does not contain any methods.

👉 Its purpose is to indicate that a class allows cloning.

If a class does NOT implement Cloneable and you call clone(), Java throws:

CloneNotSupportedException

🔹 How Cloning Works Internally

  1. The clone() method is defined in the Object class.

  2. It creates a shallow copy of the object.

  3. If the class implements Cloneable, cloning is allowed.

  4. Otherwise, it throws an exception.


🔹 Example of Cloning

class Student implements Cloneable {
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public static void main(String[] args) throws Exception {
        Student s1 = new Student(101, "Harish");
        Student s2 = (Student) s1.clone();

        System.out.println(s1.id + " " + s1.name);
        System.out.println(s2.id + " " + s2.name);
    }
}

What Happens Here?

  • s2 is a new object.

  • It contains the same values as s1.

  • By default, this is a shallow copy.


🔹 Important Points About Cloning

clone() method belongs to Object class
✔ Class must implement Cloneable
✔ Must override clone() method
✔ Default cloning is shallow copy
clone() returns Object type (needs casting)


🔹 Shallow Copy vs Deep Copy in Cloning

By default:

super.clone();

Performs Shallow Copy

If the object contains reference variables, both objects share the same reference.

To create a Deep Copy, you must manually clone nested objects.


🔹 Example of Deep Copy in Cloning

class Address {
    String city;

    Address(String city) {
        this.city = city;
    }
}

class Employee implements Cloneable {
    int id;
    Address address;

    Employee(int id, Address address) {
        this.id = id;
        this.address = address;
    }

    protected Object clone() throws CloneNotSupportedException {
        Address newAddress = new Address(this.address.city);
        return new Employee(this.id, newAddress);
    }
}

Now the copied object is completely independent.


🔥 Interview Follow-Up Questions

Interviewers may ask:

  • Why is Cloneable a marker interface?

  • Why is clone() protected in Object class?

  • What happens if we don’t implement Cloneable?

  • Why is cloning considered broken in Java?

  • Difference between clone() and copy constructor?

  • Is cloning recommended in modern Java?


🔹 Why Cloning is Considered Problematic

Many developers avoid cloning because:

  • It breaks encapsulation

  • It performs shallow copy by default

  • It requires exception handling

  • It is considered poorly designed API

Modern alternatives:

✔ Copy constructor
✔ Factory methods
✔ Serialization-based deep copy
✔ Builder pattern


🎯 Final Summary

  • Cloning = Creating copy of object

  • Cloneable = Marker interface that allows cloning

  • clone() method is defined in Object class

  • Default cloning is shallow copy

  • Deep copy requires manual implementation

  • Modern Java prefers alternatives over cloning

Cloning is still important for interviews and understanding object memory behavior.


🚀 Master Core & Advanced Java with Real-Time Projects

Understanding cloning, object lifecycle, serialization, JVM internals, and memory management is crucial for cracking technical interviews and building enterprise applications.

If you want hands-on learning with industry-level implementation, check out:

🔥 AI powered Java Real Time Projects Online Training in Hyderabad

In this program, you will:

✔ Work on real-time enterprise projects
✔ Learn advanced Core Java concepts
✔ Master cloning, serialization & JVM internals
✔ Build Spring Boot & Microservices applications
✔ Gain AI-integrated backend development skills
✔ Prepare confidently for interviews

Strong fundamentals + real-time implementation = Career growth 🚀


Thread vs Runnable in Java: Key Differences, Best Practices & Which One to Use (2026 Guide)

Multithreading is a core part of Java —but one of the most common interview questions is: Should you use Thread or Runnable ? Thread is a ...