The method clone() from object is not visible?

JavaMethodsCloneCloneable

Java Problem Overview


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.
	}
}

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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

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

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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

Solution 8 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsekharView Question on Stackoverflow
Solution 1 - JavaEsotericMeView Answer on Stackoverflow
Solution 2 - JavaPrashant KView Answer on Stackoverflow
Solution 3 - JavaSuraj ChandranView Answer on Stackoverflow
Solution 4 - JavaspiritView Answer on Stackoverflow
Solution 5 - JavaprimeView Answer on Stackoverflow
Solution 6 - JavaSwagatikaView Answer on Stackoverflow
Solution 7 - JavaYanki TwizzyView Answer on Stackoverflow
Solution 8 - JavaFocalPointView Answer on Stackoverflow