jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes protected specifier for Object class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "protected specifier for Object class" Watch "protected specifier for Object class" New topic

protected specifier for Object class

gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
when i try to call clone() method(protected) of Object class in main() through one of the objects of a class which i ve not declared public,
it gives me following error
D:\gaurav\java\certification\practice>javac ErrorRpt.java
ErrorRpt.java:13: clone() has protected access in java.lang.Object
Object objClone2 = obj2.clone();
1 error

my whole code is :
class SmDrivedClass
void smDerivedmethod(){}
public void smMthod(){}
public class ErrorRpt{
public static void main(String[] args){
ErrorRpt obj = new ErrorRpt();
SmDrivedClass obj2 = new SmDrivedClass();
Object objClone = obj.clone();///works fine
Object objClone2 = obj2.clone();//error
}catch(CloneNotSupportedException e){

but if the object is created using a public class and then i call clone method it works fine.

although as per my understanding protected means all the classes in same package or all derived classes can use that member, it has nothing to do with the waya i define my class.

please reply......

thanks,<br />gaurav abbi
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
You need to
1. implement Clonable interface
2. implment clone method and make it public.
This is what javadocs says

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the
interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface
Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the
contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned.
Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

Alik Elzin

Joined: Sep 19, 2002
Posts: 15
Hi gaurav abbi.

Protected means that only inherited classes can use protected code.

One exception is that a class can use all of it's (private,protected,package,public) code.

Thus, it is not surprising that the first clone compiled correctly ("works fine") - it invokes a protected code but from the same class.

The second line is invoked from ErrorRpt but invokes clone of class SmDrivedClass - that is why you get an error in compilation.

If you do not mark a class to be Clonable ("implements Clonable") we will fail on runtime.

Good luck,
[ January 08, 2007: Message edited by: Alik Elzin ]
marc weber

Joined: Aug 31, 2004
Posts: 11343

See Adding cloneability to a class - Using a trick with protected from Bruce Eckel's Thinking in Java.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
thanks for the reply.
I agree. Here's the link: http://aspose.com/file-tools
subject: protected specifier for Object class
It's not a secret anymore!