protected native Object clone() throws CloneNotSupportedException;
같은 패키지안에서만 접근가능하거나, protected 클래스를 상속한 하위 클래스에서만 접근 가능 (내가 까먹었으니 다시 상기시키고 가겠다)

package protectedClassOut;
import protectedClassOut.protectedInClass.ProtectedInClass;
public class ProtectedOutClass {
protected long protectedOutClassId;
ProtectedInClass pIn = new ProtectedInClass();
ProtectedOut1Class pOut1 = new ProtectedOut1Class();
protected void protectedOutClassMethod() {
System.out.println("protectedOutClassMethod");
}
protected void protectedOutClassMethod2() {
pOut1.protectedOutClassMethod();
}
}
package protectedClassOut;
public class ProtectedOut1Class {
protected long protectedOut1ClassId;
protected void protectedOut1ClassMethod() {
System.out.println("protectedOut1ClassMethod");
}
}
package protectedClassOut.protectedInClass;
import protectedClassOut.ProtectedOutClass;
public class ProtectedInClass {
protected long protectedInClassId;
ProtectedOutClass pOut = new ProtectedOutClass();
protected void protectedInClassMethod() {
System.out.println("protectedInClassMethod");
}
protected void protectedInClassMethod2() {
}
}




package clone;
public class Apple {
String color;
int weight;
public Apple(String color, int weight) {
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public int getWeight() {
return weight;
}
public Apple clone() throws CloneNotSupportedException {
return (Apple) super.clone();
}
}
package clone;
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Apple apple = new Apple("RED",100);
Apple cloneApple = apple.clone();
System.out.println(apple.getWeight());
System.out.println(cloneApple.getWeight());
}
}


나의 예상:
실제 정답:

얕은 복사:
깊은 복사:
아래의 예제를 살펴보자
Home
package clone;
public class Home implements Cloneable {
private String name;
public Home(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package clone;
public class Human implements Cloneable {
String name;
int age;
Home home;
public Human(String name, int age, Home home) {
this.name = name;
this.age = age;
this.home = home;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Home getHome() {
return home;
}
public void setHome(Home home) {
this.home = home;
}
@Override
public Human clone() throws CloneNotSupportedException {
return (Human) super.clone();
}
}
package clone;
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Human human1 = new Human("human1",1, new Home("human1home1"));
Human human2 = human1.clone();
System.out.println("human1.hashCode(): " + human1.hashCode());
System.out.println("human2.hashCode(): " + human2.hashCode());
System.out.println("human1.name.hashCode(): " + human1.name.hashCode());
System.out.println("human2.name.hashCode(): " + human2.name.hashCode());
System.out.println("human1.home.hashCode(): " + human1.home.hashCode());
System.out.println("human2.home.hashCode(): " + human2.home.hashCode());
}
}

package clone;
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Human human1 = new Human("human1",1, new Home("human1home1"));
Human human2 = human1.clone();
human1.setName("updated Human");
human1.setAge(999);
human1.getHome().setName("updated Home");
System.out.println("human1.getName(): " + human1.getName());
System.out.println("human1.getAge(): " + human1.getAge());
System.out.println("human1.getHome().getName(): " + human1.getHome().getName());
System.out.println("human2.getName(): " + human2.getName());
System.out.println("human2.getAge(): " + human2.getAge());
System.out.println("human2.getHome().getName(): " + human2.getHome().getName());
}
}

