This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "protected specifier for Object class" Watch "protected specifier for Object class" New topic
Author

protected specifier for Object class

gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
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......


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
Greenhorn

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.

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

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
sscce.org
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
thanks for the reply.
 
 
subject: protected specifier for Object class
 
Similar Threads
finalize method
Clone Method
why can't we call clone() method?
Cloning in Java
I have some vague concept about Cloneable....