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는 적용된거긴한데, 오버라이딩해서쓰는이유가 궁금한거네.
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone()
https://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html
clone 메서드를 그렇게 만듬
오브젝트 클래스가 Cloneable 인터페이스를 서포트하지 않았을때,clone메소드를 오버라이딩한 서브클래스도 인스턴스를 복제할수없는것을 표현하기위해서 이런 예외가 발생한다. 이게 먼말임
클래스가 Cloneable인터페이스를 구현하는것으로、Object.clone() 메소드가 그 클래스의 필드로부터 필드로 복사한 인스턴스를 작성하는것이 가능하다는것을 나타낸다 Cloneable인터페이스를 구현하지않는객체에 대해서 Object객체의 clone 메소드를 호출하면 예외 CloneNotSupportedException가 발생한다. 이 인터페이스를 구현한 클래스는、public메소드로 object.clone (protected)를 오버라이딩 할 필요가 있습니다.
clone메서드를 override해도 객체 상태가 클론할수 없는 상태라면 거기서 예외를 던질수있다는말
님 protected 가 뭔지 모르는거같은데요 상속되있으면 사용가능한게 아니라 상속된 클래스 내부에서 접근할 수 있는거에요
객체 외부에서는 protected나 private나 똑같음
무슨말임? object는 모든클래스의 부모니 당연히 상속된 클래스에서 접근하는건데...
???
님 지금 ObjectDemo 클래스도 Object 클래스 상속받은거니까 Student의 protected된 메서드에 모두 접근이 가능하다고 생각하는거에요?
Student나 ObjectDemo 나 extends Object를 하고있다는거임. 그 Student s1 = new Student(); s1.clone이 안되는이유가 궁금한거임
Student의 protected가아니라 Object의 protected인데...
protected는 자식 클래스 '내부'에서만 접근 가능하다니깐요 s1.clone() 은 Student 클래스가 아닌 ObjectDemo 클래스에서 접근하는거잖아요 이건 Student 클래스 내부가 아닌 외부 클래스잖아요
Student 클래스 입장에서보면 같은 부모인 Object를 가지고 있어도 ObjectDemo 클래스는 Student 클래스와 상속 관계에 놓여있지 않아요
그럼 만약에 Student클래스 내부에서 Student s1 = new Student("egoing");만들고 s1.clone하면 사용이 가능하다는말인거임?
그것도 외부접근임 내부에서 사용한다는말은 base.~~~ 이런식으로 접근하는거
아 base가 아니라 super.~~ 이나 this.~~
this.clone말하는거아님?
아 그건 되네요 ㅎㅎ 죄송
그럼 이렇게되는거임? ObjectDemo에서 s1객체만드는것은 똑같은데 Student 클래스 내부에 this.clone이 적힌 메소드나 생성자를 설정해주고 그것이 실행가능하다는거임? private랑 같은거구나
머가된다는거임? Student클래스 내부에서 Student s1 = new Student("egoing");만들고 s1.clone하면 사용이 가능하다는말인거임?
내부에서 Student s1, s1.clone 사용가능한거 맞아
ㄳ 덕분에 좀더 알았다
정리
protectec는 클래스 외부에서는 private랑 똑같음