diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowdeepcopy/School.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowdeepcopy/School.java new file mode 100644 index 0000000000..28a751741e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowdeepcopy/School.java @@ -0,0 +1,22 @@ +package com.baeldung.shallowdeepcopy; + +class School { + private String schoolName; + public School(School sc) { + this(sc.getSchoolName()); + } + public School(String schoolName) { + this.schoolName = schoolName; + } + // standard getters and setters +} + +@Override +public Object clone() { + try { + return (School) super.clone(); + } catch (CloneNotSupportedException e) { + return new School(this.getSchoolName()); + } +} + diff --git a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowdeepcopy/Student.java b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowdeepcopy/Student.java index a3f726b64b..2fc34ba709 100644 --- a/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowdeepcopy/Student.java +++ b/core-java-modules/core-java-lang-oop-patterns/src/main/java/com/baeldung/shallowdeepcopy/Student.java @@ -1,16 +1,27 @@ package com.baeldung.shallowdeepcopy; class Student { - private String name; - private int age; - private School school; - public Student(String name, int age, School school) { - this.name = name; - this.rollno = rollno; - this.school = school; - } - public Student(Student st) { - this(st.getName(), st.getAge(), st.getSchool()); - } // standard getters and setters + private String name; + private int age; + private School school; + public Student(String name, int age, School school) { + this.name = name; + this.rollno = rollno; + this.school = school; + } + public Student(Student st) { + this(st.getName(), st.getAge(), st.getSchool()); + } // standard getters and setters } +@Override +public Object clone() { + Student student = null; + try { + student = (Student) super.clone(); + } catch (CloneNotSupportedException e) { + student = new Student(this.getName(), this.getAge(), this.getSchool()); + } + student.school= (School) this.school.clone(); + return student; +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyExampleTest.java b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyExampleTest.java index 68836a96f8..a14a621dcc 100644 --- a/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyExampleTest.java +++ b/core-java-modules/core-java-lang-oop-patterns/src/test/java/com/baeldung/shallowdeepcopy/ShallowDeepCopyExampleTest.java @@ -4,31 +4,38 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class ShallowDeepCopyExampleTest { + + @Test + public void whenPerformShallowCopy_thenObjectsNotSame() { + School school = new School("Baeldung School"); + Student originalStudent = new Student("John", 20, school); + Student shallowCopy = new Student(originalStudent.getName(), originalStudent.getAge(), originalStudent.getSchool()); + assertNotSame(shallowCopy, originalStudent); + } + + @Test public void whenModifyingActualObject_thenCopyAlsoChange() { + School school = new School("Baeldung School"); + Student originalStudent = new Student("John", 20, school); + Student shallowCopy = new Student(originalStudent.getName(), originalStudent.getAge(), originalStudent.getSchool()); + school.setSchoolName("New Baeldung School"); + assertEquals(shallowCopy.getSchool().getSchoolName(), originalStudent.getSchool().getSchoolName()); + } @Test - void testShallowCopy() { - Student originalStudent = new Student("John", 20); - Student shallowCopy = originalStudent; + public void whenModifyingActualObject_thenCloneCopyNotChange() { + School school = new School("New School"); + Student originalStudent = new Student("Alice", 10, school); + Student deepCopy = (Student) originalStudent.clone(); + school.setSchoolName("New Baeldung School"); + assertNotEquals(deepCopy.getSchool().getSchoolName(), originalStudent.getSchool().getSchoolName()); + } - shallowCopy.setName("Baeldung"); - shallowCopy.setRollno(10); - - assertEquals("Baeldung", originalStudent.getName()); - assertEquals(10, originalStudent.getRollno()); - } - - @Test - void testDeepCopy() { - Student originalStudent = new Student("John", 20); - Student deepCopy = new Student(originalStudent.getName(), originalStudent.getRollno()); - - deepCopy.setName("Baeldung"); - deepCopy.setRollno(10); - - assertEquals("John", originalStudent.getName()); - assertEquals(20, originalStudent.getRollno()); - - assertEquals("Baeldung", deepCopy.getName()); - assertEquals(10, deepCopy.getRollno()); + @Test + public void whenModifyingActualObject_thenCopyNotChange() { + School school = new School("Baeldung School"); + Student originalStudent = new Student("Alice", 30, school); + Student deepCopy = new Student(originalStudent); + school.setSchoolName("New Baeldung School"); + assertNotEquals( originalStudent.getSchool().getSchoolName(), deepCopy.getSchool().getSchoolName()); } }