• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clone: ArrayList, String, StringBuilder

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i try this code all ok.

but when i try this code:

or this:

i have error at compilation

HelloWorld.java:19: error: clone() has protected access in Object
String s1 = (String)s.clone();
^
HelloWorld.java:23: error: clone() has protected access in Object
StringBuilder sb1 = ( StringBuilder)sb.clone();

String class
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait

also
StringBuilder class

Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

also both String and StringBuilder not implement Cloneable (CloneNotSupportedException will be throw for such, but code not compile)

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.
from Object class
also Object is superclass of this classes


The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.



result:

HelloWorld.java:9: error: clone() has protected access in Object
Object o1 = o.clone();

Is this behaviour because i create my class in another package form Object /String/ StringBuilder class placed (and because metod clone is proteced and for class String/StringBuilder was not overriden with less restrictive acess)?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The clone method in the Object class has protected access which means it can only be called from within the class itself, another class in the same package or a subclass.
String and StringBuilder don't override clone, therefore the same applies when you try to call the clone method on variables of those types.
Your class is not in the same package as String or StringBuilder and does not extend them, so you can't call the clone method from within your class on variables of those types.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Joanne already mentioned the difference is in the declaration of method clone:
  • in class Object: protected Object clone() throws CloneNotSupportedException
  • in class ArrayList: public Object clone()
  • in class String & Stringbuilder: no declaration, method inherited from class Object


  • So in class ArrayList the clone method has public access, meaning you can call this method without any problem. But in class Object the method has only protected access, so calling this method is restricted to a few rules.

    I think you don't need to know the clone method for the OCAJP7 exam, but this example covers a few things you DO need to know for the OCAJP7 exam:
  • method overriding: you can have a weaker access modifier (public instead of protected) and you can decide not to declare any exception although the method in the parent class declares one
  • the protected access modifier and the restrictions when you try to invoke this method on an instance of the class itself or a subclass


  • It's very likely to get questions on both items on the exam. The latter is very rewarding to create a few tricky questions where you have to be very careful and pay close attention in order to answer them correctly.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic