Download presentation
Presentation is loading. Please wait.
Published byKonrad Enger Modified over 5 years ago
1
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Clone wars… David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Last update: 19/12/2016 ~original
2
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 saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.
3
To clone or not to clone…
Caution needed To clone or not to clone…
4
no problem if IMMUTABLE
Encapsulation lost? name “David” dob name “Gunes” dob 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
5
Regained…? 1-7-2001 1-7-2001 1-7-1800 1-7-2001 name “David” dob name
“Gunes” dob tmp {Date} p {Person} 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(); d {Date} tmp = p.getDob();
6
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} Solved if it getDob() returns a clone of p’s dob Date object. tmp {Date} d {Date} tmp = p.getDob();
7
Shallow vs. Deep Clone 1-7-2001 1-7-2001 name “David” dob p name
{Person} name “David” dob 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! name “David” dob pDeepClone {Person}
8
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!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.