[!INDEX]

  1. Definition
  2. Use
  3. copy ct example-1, name,age A copy constructor has a single parameter that is an object of the same class.
  4. Deep copy address example ( Create new memory location )
  5. Shallow Copy
  6. Deep Copy
  7. Shallow copy address example ( sharing the reference )
  8. Key Points:

1 Definition

[!NOTE]

  1. Copy Ct, used to create a new object as a copy of an existing object.

2 Use

[!NOTE]

  1. Cloning Objects:- need to create a copy of an object with the same state, especially if the object has mutable fields.
  2. Passing Objects to Methods: -When you pass objects to methods and want to ensure that the original object remains unchanged, creating a copy can be beneficial.
  3. Returning Objects from Methods: - When returning objects from methods, especially in APIs, to prevent the caller from modifying the internal state of the returned object.
  4. Immutable Objects: - To create a modified copy of an immutable object, where the new object is based on the original object with some changes.
  5. Deep Copy Requirements: - When a deep copy of an object is required, especially for objects that contain references to other objects.

3. copy ct example-1, name,age A copy constructor has a single parameter that is an object of the same class.

public class Person {
    private String name;
    private int age;

    // Parameterized constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    // Copy constructor
    public Person(Person other) {
        this.name = other.name;
        this.age = other.age;
    }
    // Getter methods
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    // Method to display person details
    public void display() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
    public static void main(String[] args) {
        // Creating an object using parameterized constructor
        Person person1 = new Person("Alice", 30);
        person1.display(); // Output: Name: Alice, Age: 30
        // Creating a new object using the copy constructor
        Person person2 = new Person(person1);
        person2.display(); // Output: Name: Alice, Age: 30
        person2.name ="ram"; // change only name , rest unchanged
        person2.display(); // Output: Name: Alice, Age: 30
    }
}

[!NOTE]

  1. other.name ka matlab other refer karta hai p1 ke name ko kyuki wo hee argument me pass ho raha hai

[!NOTE]

  1. person का name/age save करना है
  2. person class बनायेगे constructor के साथ , fields = name/age
  3. main class में p1 obj ( person class का ) बनायेगे और constructor से initilized कर देगे
  4. अब copy constructor के लीये
  5. address class में एक constructor को बनायेगे और उसमे person class का instance/ object(other ) पास कर देगे |
  6. main class में एक दुसरी object बनायेगे p2 नाम का और उसमे p1 object को पास देगे |

4. Deep copy address example ( Create new memory location )

[!NOTE]

class Address {
    String city;
    String state;
    public Address(String city, String state) {
        this.city = city;
        this.state = state;
    }
    // Copy constructor for Address
    public Address(Address other) {
        this.city = other.city;
        this.state = other.state;
    }
}
class Person {
    private String name;
    private int age;
    private Address address;

    public Person(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = new Address(address); // deep copy, independent
        //for every object, creates a new memory location, independent to each other
    }
    // Deep copy constructor
    public Person(Person other) {
        this.name = other.name;
        this.age = other.age;
        this.address = new Address(other.address); // Create a new Address object
    }
    // Display method
    public void display() {
        System.out.println("Name: " + name + ", Age: " + age + ", City: " + address.city + ", State: " + address.state);
    }
    public static void main(String[] args) {
        Address addr1 = new Address("New York", "NY");
        Person person1 = new Person("Alice", 30, addr1);
        person1.display(); // Output: Name: Alice, Age: 30, City: New York, State: NY

        Person person2 = new Person(person1);
        person2.display(); // Output: Name: Alice, Age: 30, City: New York, State: NY
        // Modifying the address of person2 should not affect person1
        person2.address.city = "Los Angeles";
        person2.display(); // Output: Name: Alice, Age: 30, City: Los Angeles, State: NY
        person1.display(); // Output: Name: Alice, Age: 30, City: New York, State: NY
    }
}

[!NOTE] Modifying the address in the copied Person object (person2) does not affect the original Person object (person1).

[!NOTE]

  1. Create address and person class.
  2. Create obj. of address and person (p1) and pass values
  3. For second person (p2), create copy constructor and pass p1 in it.

5. Shallow Copy

public Person(Person other) {
    this.name = other.name;
    this.age = other.age;
    this.address = other.address; // Shallow copy
}

[!important]

  1. This simply copies the ==reference== of the address object from other to this.
  2. Both the original (other) and the new (this) Person objects will refer to the same Address object in memory.
  3. Any changes made to the address object through one Person instance will reflect in the other.

6. Deep Copy

public Person(Person other) {
    this.name = other.name;
    this.age = other.age;
    this.address = new Address(other.address); // Deep copy
}

[!important]

  1. This creates a new Address ==object== that is a copy of the Address object referenced by other.
  2. The new Person object will have its own Address object, independent of the Address object referenced by other.
  3. Changes made to the address object of one Person instance will not affect the other.

7. Shallow copy address example ( sharing the reference )

class Address {
    String city;
    String state;

    public Address(String city, String state) {
        this.city = city;
        this.state = state;
    }
}

class Person {
    private String name;
    private int age;
    private Address address; // Mutable object

    // Parameterized constructor
    public Person(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address; // Shallow copy (sharing the reference)
    }

    // Copy constructor (shallow copy)
    public Person(Person other) {
        this.name = other.name;
        this.age = other.age;
        this.address = other.address; // Shallow copy, same Address reference
    }

    // Display method
    public void display() {
        System.out.println("Name: " + name + ", Age: " + age + ", City: " + address.city + ", State: " + address.state);
    }

    public static void main(String[] args) {
        // Creating an Address object
        Address addr1 = new Address("New York", "NY");

        // Creating two Person objects using the same Address
        Person person1 = new Person("Alice", 30, addr1);
        Person person2 = new Person(person1); // Shallow copy of person1

        // Displaying both persons
        System.out.println("Before changing the address:");
        person1.display(); // Name: Alice, Age: 30, City: New York, State: NY
        person2.display(); // Name: Alice, Age: 30, City: New York, State: NY

        // Changing the city in person2's address
        person2.address.city = "Los Angeles"; 

        // Displaying both persons again
        System.out.println("\nAfter changing the address in person2:");
        person1.display(); // Name: Alice, Age: 30, City: Los Angeles, State: NY
        person2.display(); // Name: Alice, Age: 30, City: Los Angeles, State: NY
    }
}

8. Key Points:

[!important]

  1. In the first example (with Address):
    • Address is mutable.
    • person1 and person2 share the same Address instance after the shallow copy.
    • Changing the Address (like city) in one object affects both because both reference the same mutable object.
  2. In the second example (with String):
    • String is immutable.
    • Even though person1 and person2 share the same name reference initially, changing the name for person2 results in a new String object being assigned, so person1.name remains unchanged.