class Point extends java.lang.Object implements Cloneable {
private int xPos;
private int yPos;
public Point(int x, int y) {
xPos = x;
yPos =y;
}
public void showPosition() {
System.out.printf("[%d, %d]", xPos, yPos);
System.out.println();
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class InstanceCloning {
public static void main(String[] args) {
Point org = new Point(3, 5);
Point cpy;
try {
cpy = (Point)org.clone();
org.showPosition();
cpy.showPosition();
}
catch(CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
여기서 이해가 안가는게 모든 클래스는 object 클래스를 상속하니까 clone 함수를 오버라이딩 하지 않아도 clone함수를 호출 할 수 있는 것 아닌가요?
제가 이해가기로는 object클래스가 다른 패키지에 있더라도 protected로 선언이 되어있기 때문에 상속하는 클래스에서 호출을 할 수 있어야 하는데
컴파일 해보니까 오류가 나고 이해가 안되서 여기다가 질문드립니다 ㅠㅠㅠㅠㅠ
댓글 0