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
?