/* illustrate the concept of clone() in java */
/*
Cloneable is an interface .
*/
package clonemethod;
class Student implements Cloneable
{
int roll;
String name;
Student(int roll,String name)
{
this.roll=roll;
this.name=name;
System.out.println("details are : "+roll+" "+name);
}
public Object clone()throws CloneNotSupportedException{
return super.clone();
}
public static void main(String args[]){
try{
Student s1=new Student(21,"sanjay");
Student s2=(Student)s1.clone();
System.out.println(s1.roll+" "+s1.name);
System.out.println(s2.roll+" "+s2.name);
}catch(CloneNotSupportedException c){}
}
}
/*
Cloneable is an interface .
*/
package clonemethod;
class Student implements Cloneable
{
int roll;
String name;
Student(int roll,String name)
{
this.roll=roll;
this.name=name;
System.out.println("details are : "+roll+" "+name);
}
public Object clone()throws CloneNotSupportedException{
return super.clone();
}
public static void main(String args[]){
try{
Student s1=new Student(21,"sanjay");
Student s2=(Student)s1.clone();
System.out.println(s1.roll+" "+s1.name);
System.out.println(s2.roll+" "+s2.name);
}catch(CloneNotSupportedException c){}
}
}
Output
details are : 21 sanjay
21 sanjay
21 sanjay
No comments:
Post a Comment