Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[ Protected methods ] Don't understand error message

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Art Metzer
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic