|
| |||||||||
| Tags: classes, cloning, function, method, utilities, variables |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Cloning in Java Cloning in Java 1. Introduction: In object-oriented programming, sometimes we should clone an object. The "cloning" of an object could be defined as the creation of a copy of that value object. This tutorial shows how this concept is implemented in Java. Note- for better understanding of this concept one should have a good knowledge of Java Core. 2. The Interface Cloneable: To be cloned, a class must implement the interface Cloneable. This indicates that the class can rewrite the clone() method inherited from the class Object, so that their bodies can make a copy of its attributes to a new instance (a copy of the object by value). By convention, classes that implement the interface Cloneable must rewrite the method Object clone() (which is protected) With visibility Public. Note that the interface Cloneable does not contain the clone() method. Therefore, it is impossible to clone an object if its class does not implement this interface. Even if the clone() method is called by reflection, its success is not guaranteed. A class implementing the interface Cloneable must necessarily rewrite method clone() to be cloned, in contrast, class rewriting the method clone() must implement the interface Cloneable on pain of being left with a CloneNotSupportedException. |
|
#2
| |||
| |||
| Re: Cloning in Java 3. The method Clone(): A class implementing the interface Cloneable must rewrite the method clone(). The clone() method must return a copy of the object that you want to clone. This copy depends on the class of the object, but it must meet the conditions. For an object "clonable" o: The expression is Code: o.clone() != o The expression is Code: o.clone().getClass() == o.getClass() The expression is Code: o.clone().equals(o) By convention, the returned object must be obtained by a method call super.clone (). If a class and all its superclasses (except class Object) Follow this convention, then the expression Code: o.clone().getClass() == o.getClass() By convention, the returned object must be independent of the cloned object, i.e all not immutable attributes will also be cloned. It must also know the default implementation of clone() (clone() Class Object). The method clone() in class Object performs a specific cloning operation. In a first time, if the class of the object does not implement interface Cloneable then a CloneNotSupportedException rose. Otherwise, a new instance of the class of the object is created and the Attributes are initialized with those of the cloned object. However a copy of these attributes is through reference and not by value (they are not cloned!). So, to summarize the important points of the rewriting of the clone() method are: - retrieve the object returned by calling the super.clone() - clone not immutable attributes to move from one area to copy a copy depth of the object. |
|
#3
| |||
| |||
| Re: Cloning in Java 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;
}
} 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);
}
} |
|
#4
| |||
| |||
| Re: Cloning in Java 5. An Attribute Not Clonable: In case your class has an attribute not immutable and not clonable, you just can not clone. Although you can use a copy constructor your attribute (if it has one), you can find where your two object (original clone) has the same attribute (same instance). Note that this is not "abnormal" (do not panic if it appears to you), it is indeed possible that we want to deliberately keep the same instance of an attribute. After all, this is perhaps not for nothing that it does not implement interface Cloneable. 6. Inherit a Class of Non-Clonable: In case your class inherits a class of non-clonable, know that the method call super.clone () returns to execute the method clone () default. This means that non-static attributes of the superclass will not be cloned. It is your responsibility to clone them, unless they are inaccessible (reported private) Or not clonable. |
|
#5
| |||
| |||
| Re: Cloning in Java 7. Remarks Note that the Object class does not implement interface Cloneable, Then call the method clone () on an object whose class is Object invariably rise CloneNotSupportedException. Note also that the tables are regarded as implementing the interface Cloneable. However is the method clone() default is used, objects in two tables are copied by reference. Code: Public class CloneTableaux {
Public static void hand(String [] args) {
Nobody prs1 = new Person(new Surname("s1", "s2"), 31);
Nobody prs2 = new Person(new Surname("s", "S2"), 32);
Person [] arr1 = { prs1, prs2 };
Person [] arr2 = (Person []) arr1.clone();
System.out.System.out.println("arr1 : " + arr1);
System.out.System.out.println("arr2 : " + arr2);
System.out.System.out.println("arr1 [0] : " + arr1 [0]);
System.out.System.out.println("arr2 [0] : " + arr2 [0]);
}
} Code: Public Surname clone() {
Patronymic srnm = null;
try {
srnm = (Surname) great.clone();
} catch(CloneNotSupportedException e) {
e.printStackTrace(System.err);
}
return srnm;
}
// Method clone () of the Class Person
// Returns now a object of type Person and no more of type Object
Public Person clone() {
One prs = null;
try {
prs = (Person) great.clone();
} catch(CloneNotSupportedException e) {
e.printStackTrace(System.err);
}
// More need of cast for clone the srnm.
prsne.patronyme = srnm.clone();
return prs;
}
// Method clone () of the Class Toy
// Returns now a object of type Toy and no more of type Object
Public Toy clone() {
Toy ty = null;
try {
ty = (Toy) great.clone();
} catch(CloneNotSupportedException e) {
e.printStackTrace(System.err);
}
return ty;
}
// Method clone () of the Class Child
// Returns now a object of type Child and no more of type Object
Public Child clone() {
Child ch = (Child) great.clone();
// More need of cast for clone the ty.
enfant.jpref = jpref.clone();
return ch;
} Code: Public static void hand(String [] args) {
Nobody prs1 = new Person(new Surname("s1", "s2"), 30);
// More need of cast for clone the prs.
Nobody prs2 = prs1.clone();
System.out.System.out.println(prs1);
System.out.System.out.println(prs2);
Child ch1 = new Child(new Surname("s", "s2"), 10, new Toy("Teddy bear"));
// More need of cast for clone the ch.
Child ch2 = ch1.clone();
System.out.System.out.println(ch1);
System.out.System.out.println(ch2);
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Cloning in Java" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| what is sim cloning?? | Bronoe | Portable Devices | 6 | 06-01-2012 07:08 AM |
| Cloning from IDE to SATA | Iraivan | Hardware Peripherals | 6 | 29-12-2010 07:14 PM |
| Disc Cloning in MAC | Edwards | Windows Software | 5 | 15-02-2010 10:00 AM |
| Cloning software | kaviken | Windows Software | 4 | 01-08-2009 02:33 PM |
| Cloning datarow in .net | afidelino | Software Development | 3 | 29-07-2009 01:51 PM |