aspose file tools*
The moose likes Beginning Java and the fly likes [ Protected methods ] Don't understand error message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[ Protected methods ] Don Watch "[ Protected methods ] Don New topic
Author

[ Protected methods ] Don't understand error message

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

I have this sample code:
class A{
private int _i;
public A(int i) { _i = i; }
int getA(){ return _i; }
}
public class CloneDemo{
public static void main(String args[]){
A a1 = new A(99);
A a2 = (A)a1.clone();
}
}
This code gives a compile error:
10: Can't access protected method clone in class java.lang.Object
. A is not a subclass of the current class.
A a2 = (A)a1.clone();
^
ok. So Object.clone() is a protected method. But all classes
are by default subclasses of "Object". What does the JVM mean by:
"A is not a subclass of the current class" ? What is the current class - "Object" ?
Thanks
Pho

Regards,

Pho
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Pho:
The clone() method in java.lang.Object is protected. This encapsulation prevents default cloneability in classes. In some cases, you may not want an instance of a class to be cloned. If Object's clone() method were public, it could be accessed from anywhere. Subclasses cannot reduce the access of a method while overriding it (e.g., overriding a public method to be protected). Therefore, if clone were public in Object, all classes would automatically be cloneable.
One of the consequences of having Object's clone() method be protected is that you can not write general-purpose methods that take an Object as a parameter and clone the Object.
There is a way around this, however, Pho. Normally, in order to make a class cloneable, it must implement the interface Cloneable and override Object's clone() method so that it is public. If you try to clone an Object directly, your program will not compile, generating the error that we've seen.
Try this code on for size:

Hope this helps, Pho.
Art
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

If I understand you correctly, the protected method clone()
is actually available to any subclass of Object. But it can only be used in the form:

inside the subclass.
And to make this available to the user, we create a new
public method. So we are in affect, changing the access specifier from protected to public. I get it!
Thanks
Pho
Ajit Kanada
Ranch Hand

Joined: Jan 23, 2001
Posts: 95
see what i have i can access protected method of super class from
derived class...this code does this
but why it is not possible for clone() method?
class A
{
protected void m()
{System.out.println("i m protrcrd ..");}
}
class CloneDemo extends A
{
public static void main(String [] a)
{
CloneDemo d= new CloneDemo();
d.call();
}
public void call()
{
m();
}
}
pl reply
Ajit


Thankx
Ajit
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It is possible for the clone() method but that wasn't what your example did. A could run super.clone(). The class CloneDemo can't run A's super.clone().


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [ Protected methods ] Don't understand error message
 
Similar Threads
Garbage collection query
exam lab gc question
Why Clone method is Protected
Protected
simple........