The method clone() from object is not visible?

感情迁移 提交于 2019-11-27 05:36:19

问题


Question:

package GoodQuestions;
public class MyClass {  
    MyClass() throws CloneNotSupportedException {
        try {
            throw new CloneNotSupportedException();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }   

    public static void main(String[] args) {    
        try {
            MyClass  obj = new MyClass();
            MyClass obj3 = (MyClass)obj.clone();            
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

Here class 'MyClass' can able to clone its own object by calling the clone method in 'Object' class. When I try to clone the of this class('MyClass') in another class('TestSingleTon') in the same package 'GoodQuestions' it is throwing the following compile time error.

'The method clone() from the type Object is not visible'

So here is the code it throwing the above error?

package GoodQuestions;
public class TestSingleTon {
    public static void main(String[] args) {
        MyClass  obj = new MyClass();
        MyClass obj3 = obj.clone(); ---> here is the compile error.
    }
}

回答1:


This error occurs because in Object class clone() method is protected. So you have to override clone() method in respective class. Eg. Add below code in MyClass

@Override
protected Object clone() throws CloneNotSupportedException {

    return super.clone();
}

Also implement Cloneable interface. Eg. public class MyClass implements Cloneable




回答2:


clone() has protected access. Add this in MyClass

public Object clone(){  
    try{  
        return super.clone();  
    }catch(Exception e){ 
        return null; 
    }
}

Also Change to public class MyClass implements Cloneable




回答3:


Because clone() is a protected method. See Object.clone() for details.

Override clone() in MyClass and make the class implement Cloneable interface.




回答4:


You just have to make MyClass implement Cloneable interface. No need to provode implementation for clone().




回答5:


The subtlety is that the clone() method of MyClass is inherited, not defined in MyClass. So MyClass can invoke clone() of the Object because it is protected, but MyClass doesn't really have a clone() of itself, so TestSingleTon can't access clone() of MyClass because there is no clone() method. Although they are both in a same package, you need to define a clone() method in MyClass to assure it really "has" the clone(). By the way, don't forget to implement the interface Cloneable for MyClass.




回答6:


Object.clone() method has protected access, meaning it's visible to sub-classes and classes in the same package.

It's good to have a copy constructor for manually copying the object.

/**
    Deep copy all the information from other to this
*/
public MyClass (MyClass  other) {
     this.id = other.id;
}

READ Why a copy constructor from Josh Bloch




回答7:


For you to be able to clone MyClass, it has to implement the Cloneable interface




回答8:


I did some test code on this and here are my findings:

When a protected member is inherited across package it becomes private member of inherited class

whereas

when a protected member is inherited within the same package it becomes default member of inherited class.

In your example, clone() from Object class is inherited into MyClass across package. Object class is in java.lang package and MyClass is in GoodQuestions package. So clone() method becomes a private member of MyClass class.

That explains why you are unable to access clone() method from TestSingleTon class.



来源:https://stackoverflow.com/questions/5116264/the-method-clone-from-object-is-not-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!