Java Coding 6_part3 David Davenport Computer Eng. Dept., Clone wars… David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Last update: 19/12/2016 ~original
IMPORTANT… Students… Instructors… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.
To clone or not to clone… Caution needed To clone or not to clone…
no problem if IMMUTABLE Encapsulation lost? name “David” dob name “Gunes” dob 1-7-2001 1-7-1800 Create date d, then use it to create Persons p & p2… If constructor simply copies reference, i.e. this.dob = dob; result is that d holds a reference to one of p’s properties & can tweak it, thus breaking encapsulation! Note: there is no problem with primitive types or immutable classes IMMUTABLE objects ~ones whose state cannot change after creation Java Strings are immutable, so the name property is safe! p {Person} p2 {Person} d {Date} no problem if IMMUTABLE
Regained…? 1-7-2001 1-7-2001 1-7-1800 1-7-2001 name “David” dob name “Gunes” dob tmp {Date} p {Person} 1-7-1800 p2 {Person} Cloning d in the Person constructor is safer, i.e. this.dob = d.clone(); or this.dob = new Date( dob); But accessor methods can again cause “problems”… e.g. if Person’s getDob() method simply returns the reference to the Date property, i.e. return p.getDob(); 1-7-2001 d {Date} tmp = p.getDob();
Clones everywhere…? 1-7-2001 1-7-2001 1-7-2001 1-7-1800 1-7-2001 name “David” dob p {Person} name “Gunes” dob p2 {Person} 1-7-2001 1-7-2001 Solved if it getDob() returns a clone of p’s dob Date object. 1-7-2001 tmp {Date} 1-7-1800 1-7-2001 d {Date} tmp = p.getDob();
Shallow vs. Deep Clone 1-7-2001 1-7-2001 name “David” dob p name {Person} name “David” dob 1-7-2001 pShallowClone {Person} Similar problems can occur when cloning objects with object-type properties. Shallow clone ~ object-type properties copied by reference only (clone shares property object!) Deep clone ~ object-type properties also cloned ( clones completely separate! ) Java objects all have: toString, equals, clone but may not do what you expect or want ~ clone is usually shallow clone! 1-7-2001 name “David” dob pDeepClone {Person}
but may not do what you want/expect! Shallow vs. Deep clone x Java objects have toString equals clone y Similar problems can occur when cloning objects with object-type properties. Shallow clone ~ object-type properties copied by reference only (clone shares property object!) Deep clone ~ object-type properties also cloned ( clones completely separate! ) Java objects all have: toString, equals, clone but may not do what you expect or want ~ clone is usually shallow clone! y but may not do what you want/expect!