2

I am testing the behaveour of clone() method.but when I am creating clone of each object of ArrayList this is showing compile time error-

The method clone() from the type Object is not visible

I have two question-

Question:1- Is this due to clone method is protected in Object class.so we have to override it(define same signeture) or we have two overload it in Language class(non protected version-since they are in different package).Is it necessery to define clone in Language class?

my CloneTesting.java file code is-

package pack1;
import java.util.ArrayList;
class CloneTesting {  
  public static void main(String args[]) throws ClassNotFoundException{

      ArrayList<Language> list=new ArrayList<Language>();
      list.add(new Language("C","1"));
      list.add(new Language("JAVA","2"));
      list.add(new Language("C#","3"));
      list.add(new Language(".Net","4"));
      list.add(new Language("C++","5"));

      ArrayList<Language> list1=(ArrayList<Language>) list.clone();
      System.out.println(list==list1);

      ArrayList<Language> list2=new ArrayList<Language>();
      for(Language language:list){
          list2.add((Language) language.clone());
      }
      System.out.println(list==list2);
   } 
}

my Language.java class code is-

package pack2;
public class Language implements Cloneable{

    private String name;
    private String id;
    public Language(String name, String id){
        this.name=name;
        this.id=id;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }
}

Ques:2- what will be difference in behaveour of clone- list1 and clone- list2?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222

1 Answers1

0

Question:1- Is this due to clone method is protected in Object class.so we have to override it(define same signeture) or we have two overload it in Language class(non protected version-since they are in different package).Is it necessery to define clone in Language class?

The compiler complains because Language doesn't implement clone(). So it uses the clone() method from Object class. Since Object class is in a different package, the compiler doesn't allow you to call clone() on Language. If you override clone() in Language, then the error will not be shown.

Ques:2- what will be difference in behaveour of clone- list1 and clone- list2?

In case of list1 you are doing shallow copy of the list. So there is no way of getting deep copy of objects inside the list. In case of list-2 if clone() is not overriden, then shallow copy of each element is done, if clone() is overriden then you could get deep copy of each object inside the list.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • Is default clone() method provided by eclipse will be sufficient?also since class Language is in different package so how clone method define in Language class will defer from same define in object class? –  May 06 '15 at 17:09
  • 1
    @dubey-theHarcourtians - You will have to *override* `clone()` in `Language` and make it `public` so that it can be accessed from different packages. Since `Language` doesn't define a `clone()`, the`clone()` of `Object` is used. If you look at the *error* it says *from class Object* because you are trying to access `clone()` of `Object` via a reference of `Language`. – TheLostMind May 07 '15 at 05:56