[!INDEX]
- Definition
- Use
- copy ct example-1, name,age A copy constructor has a single parameter that is an object of the same class.
- Deep copy address example ( Create new memory location )
- Shallow Copy
- Deep Copy
- Shallow copy address example ( sharing the reference )
- Key Points:
1 Definition
[!NOTE]
- Copy Ct, used to create a new object as a copy of an existing object.
2 Use
[!NOTE]
- Cloning Objects:- need to create a copy of an object with the same state, especially if the object has mutable fields.
- 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.
- 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.
- Immutable Objects: - To create a modified copy of an immutable object, where the new object is based on the original object with some changes.
- 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]
- other.name ka matlab other refer karta hai p1 ke name ko kyuki wo hee argument me pass ho raha hai
[!NOTE]
- person का name/age save करना है
- person class बनायेगे constructor के साथ , fields = name/age
- main class में p1 obj ( person class का ) बनायेगे और constructor से initilized कर देगे
- अब copy constructor के लीये
- address class में एक constructor को बनायेगे और उसमे person class का instance/ object(other ) पास कर देगे |
- main class में एक दुसरी object बनायेगे p2 नाम का और उसमे p1 object को पास देगे |
4. Deep copy address example ( Create new memory location )
[!NOTE]
- Consider a situation where you have a
Personclass with anAddressfield, and you want to create a copy of aPersonobject with a different address:
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
Personobject (person2) does not affect the originalPersonobject (person1).
[!NOTE]
- Create address and person class.
- Create obj. of address and person (p1) and pass values
- 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]
- This simply copies the ==reference== of the
addressobject fromothertothis.- Both the original (
other) and the new (this)Personobjects will refer to the sameAddressobject in memory.- Any changes made to the
addressobject through onePersoninstance 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]
- This creates a new
Address==object== that is a copy of theAddressobject referenced byother.- The new
Personobject will have its ownAddressobject, independent of theAddressobject referenced byother.- Changes made to the
addressobject of onePersoninstance 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]
- In the first example (with
Address):
Addressis mutable.person1andperson2share the sameAddressinstance after the shallow copy.- Changing the
Address(likecity) in one object affects both because both reference the same mutable object.- In the second example (with
String):
Stringis immutable.- Even though
person1andperson2share the samenamereference initially, changing thenameforperson2results in a newStringobject being assigned, soperson1.nameremains unchanged.