package org.opentutorials.javatutorials.progenitor;
class Student implements Cloneable{
String name;
Student(String name){
this.name = name;
}
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
class ObjectDemo {
public static void main(String[] args) {
Student s1 = new Student("egoing");
try {
Student s2 = (Student)s1.clone();
System.out.println(s1.name);
System.out.println(s2.name);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}


위에 왜 clone을 오버라이딩해줘야하는거임?


package org.opentutorials.javatutorials.progenitor;
class Student implements Cloneable{
String name;
Student(String name){
this.name = name;
}
}
class ObjectDemo {
public static void main(String[] args) {
Student s1 = new Student("egoing");
try {
Student s2 = (Student)s1.clone();
System.out.println(s1.name);
System.out.println(s2.name);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}

그냥 위에처럼하면안된느거임?


protected (상속했을 경우 접근가능)

보호된,부모로 부터 보호(상속), 다른패키지의 서브클래스를 상속했을시에는 사용가능


다른패키지의 서브클래스(object)를 Student가 상속하고있음.
1.오브젝트의 메소드 protected Object clone() 을 사용위해서, Student implements cloneable을 해줌
2.Student s1 = new Student("egoing");객체를 만들어서 s1.clone()으로 사용안되고 위에처럼한 이유가 머임?
익셉션 처리한다고쳐도 s1.clone이 사용안되는게 이상함

왜냐면 protected니까 다른패키지인데 상속되있으니까 사용가능할거아님?

그러고보니 오버라이딩 되는것부터 protected는 적용된거긴한데, 오버라이딩해서쓰는이유가 궁금한거네.