#javaDay28Part2

Publié le: 06 juin 2024
sur la chaîne: Geeks With Geeks
12
0

Java Object Cloning is a process that creates an exact copy of an object. Cloning is useful when you need to duplicate an object without affecting the original instance. In Java, object cloning can be achieved in several ways, most notably through the `Cloneable` interface and by using the `clone()` method provided by the `Object` class.

Here's a detailed breakdown:

1. Shallow Cloning
Shallow cloning creates a new instance of the object and copies all fields to the new instance. However, if the original object contains references to other objects, only the references are copied. This means both the original and the cloned objects will reference the same nested objects.

#### How to Implement Shallow Cloning
1. **Implement the `Cloneable` Interface**: Your class must implement the `Cloneable` interface. This is a marker interface, meaning it doesn’t have any methods to implement but indicates that the class allows cloning.
2. **Override the `clone()` Method**: Override the `clone()` method from the `Object` class. Inside this method, call `super.clone()` to create the clone.

Here’s an example:

```java
class Person implements Cloneable {
String name;
int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

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

@Override
public String toString() {
return "Person[name=" + name + ", age=" + age + "]";
}
}

public class Main {
public static void main(String[] args) {
try {
Person p1 = new Person("John", 25);
Person p2 = (Person) p1.clone();

System.out.println(p1); // Output: Person[name=John, age=25]
System.out.println(p2); // Output: Person[name=John, age=25]

p2.name = "Doe";
System.out.println(p1); // Output: Person[name=John, age=25]
System.out.println(p2); // Output: Person[name=Doe, age=25]
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
```

2. Deep Cloning
Deep cloning involves creating a new instance of the object and also cloning the objects referenced by the original object. This means the cloned object and the original object do not share any references to other objects.

#### How to Implement Deep Cloning
To achieve deep cloning, you need to clone all referenced objects manually. This might involve overriding the `clone()` method in those referenced objects as well.

Here’s an example:

```java
class Address implements Cloneable {
String city;
String country;

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

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

@Override
public String toString() {
return "Address[city=" + city + ", country=" + country + "]";
}
}

class Person implements Cloneable {
String name;
int age;
Address address;

public Person(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}

@Override
protected Object clone() throws CloneNotSupportedException {
Person cloned = (Person) super.clone();
cloned.address = (Address) address.clone();
return cloned;
}

@Override
public String toString() {
return "Person[name=" + name + ", age=" + age + ", address=" + address + "]";
}
}

public class Main {
public static void main(String[] args) {
try {
Address addr1 = new Address("New York", "USA");
Person p1 = new Person("John", 25, addr1);
Person p2 = (Person) p1.clone();

System.out.println(p1); // Output: Person[name=John, age=25, address=Address[city=New York, country=USA]]
System.out.println(p2); // Output: Person[name=John, age=25, address=Address[city=New York, country=USA]]

p2.name = "Doe";
p2.address.city = "Los Angeles";
System.out.println(p1); // Output: Person[name=John, age=25, address=Address[city=New York, country=USA]]
System.out.println(p2); // Output: Person[name=Doe, age=25, address=Address[city=Los Angeles, country=USA]]
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}


Sur cette page du site, vous pouvez voir la vidéo en ligne #javaDay28Part2 durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Geeks With Geeks 06 juin 2024, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 12 fois et il a aimé 0 téléspectateurs. Bon visionnage!