Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected specifier for Object class

 
gaurav abbi
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
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();
obj2.smDerivedmethod();
try{
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......
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

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

Good luck,
Alik.
[ January 08, 2007: Message edited by: Alik Elzin ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Adding cloneability to a class - Using a trick with protected from Bruce Eckel's Thinking in Java.
 
gaurav abbi
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic