4. Some of the Example are shown here
Code:
Public class Surnm implements Cloneable {
private String frsnm;
private String nm;
Public Surnm(String frsnm, String nm) {
this. first_nm = surnm;
this. nm = nm;
}
Public Object clone() {
Object o = null;
try {
// We recovers proceeding to return by Call of the
// method super.clone ()
o = great.clone();
} catch(CloneNotSupportedException CNSE) {
// Do should never arrive because we are implementing
// interface Cloneable
CNSE.printStackTrace(System.err);
}
// we returns the clone
return o;
}
}
Code:
Public class Person implements Cloneable {
private Patronymic srnm;
private int ag;
Public Person(Surname surname, int ag) {
this. patronymic = surname;
this. ag = ag;
}
Public Object clone() {
One prs = null;
try {
// We recovers proceeding to return by Call of the
// method super.clone ()
prs = (Person) great.clone();
} catch(CloneNotSupportedException e) {
// Do should never arrive because we are implementing
// interface Cloneable
e.printStackTrace(System.err);
}
// We clone attribute of type Surname who is not immutable.
prsne.patronyme = (Surname) surname.clone();
// we returns the clone
return prs;
}
}
Code:
Public class Toy implements Cloneable {
private String nm;
Public Toy(String nm) {
this. nm = nm;
}
Public Object clone() {
Object o = null;
try {
// We recovers proceeding to return by Call of the
// method super.clone ()
o = great.clone();
} catch(CloneNotSupportedException e) {
// Do should never arrive because we are implementing
// interface Cloneable
e.printStackTrace(System.err);
}
// we returns the clone
return o;
}
}
Code:
Public class Child extends Person {
private Toy jpref;
Public Child(Surname srnm, int ag, Toy jpref) {
great(srnm, ag);
this. jpref = jpref;
}
Public Object clone() {
// We recovers proceeding to return by Call of the
// method super.clone () (here : Personne.clone ())
Child ch = (Child) great.clone();
// We clone attribute of type Toy who is not immutable.
enfant.jpref = (Toy) jpref.clone();
// we returns the clone
return ch;
}
}
And, finally the main method
Code:
Public class CloneMain {
Public static void hand(String [] args) {
Nobody pr1 = new Person(new Surname("Vijay", "Amit"), 30);
Nobody person2 = (Person) pr1.clone();
System.out.System.out.println(pr1);
System.out.System.out.println(person2);
Child ch1 = new Child(new Surname("Nachi", "Dom"), 10, new Toy("Teddy bear"));
Child ch2 = (Child) ch1.clone();
System.out.System.out.println(ch1);
System.out.System.out.println(ch2);
}
}
Bookmarks