SlideShare una empresa de Scribd logo
1 de 27
Effective Java   - Item 10 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Item 10: Override clone judiciously ,[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],assume: default constructor reads data from file Problem: breaks encapsulation!
[object Object],[object Object],[object Object],[object Object],[object Object]
Swimmer String name swimTime eventDate 26.31 Kristen Date year 2005 etc. 1982 Immutable mutable cu1 d
Aliasing problem ,[object Object],[object Object],[object Object],[object Object],[object Object]
protected  Object   clone () throws    CloneNotSupportedException ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Object.clone() ,[object Object],[object Object]
Cloneable interface ,[object Object],[object Object],[object Object],[object Object]
Example:  Class Die  ( not implementing Cloneable ) ,[object Object],[object Object],[object Object],[object Object],[object Object],note the cast (since clone returns Object)
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],calls Object.clone() can’t happen, since Die is Cloneable
Cloning swimmer: ,[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Cloning swimmers: ,[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],makes a bit-by-bit copy copies the Date
It is up to the class designer to determine whether: ,[object Object],[object Object],[object Object],[object Object],[object Object]
Case 1: shallow cloning ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Case 2: deep cloning ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],fix the accessor by returning a copy
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Case 3: don’t use cloning ,[object Object],[object Object],[object Object]
Advantages over cloning… ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object]
Thank You

Más contenido relacionado

La actualidad más candente

Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»
Anna Shymchenko
 
Advanced Hibernate
Advanced HibernateAdvanced Hibernate
Advanced Hibernate
Haitham Raik
 
Pharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkPharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source Smalltalk
Serge Stinckwich
 
Advanced Hibernate V2
Advanced Hibernate V2Advanced Hibernate V2
Advanced Hibernate V2
Haitham Raik
 
Real world cross-platform testing
Real world cross-platform testingReal world cross-platform testing
Real world cross-platform testing
Peter Edwards
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
Deepak Singh
 
Servletand sessiontracking
Servletand sessiontrackingServletand sessiontracking
Servletand sessiontracking
vamsi krishna
 

La actualidad más candente (18)

Head First Java Chapter 1
Head First Java Chapter 1Head First Java Chapter 1
Head First Java Chapter 1
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
 
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»
 
JavaScript Common Mistake
JavaScript Common MistakeJavaScript Common Mistake
JavaScript Common Mistake
 
Advanced Hibernate
Advanced HibernateAdvanced Hibernate
Advanced Hibernate
 
Pharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkPharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source Smalltalk
 
Advanced Hibernate V2
Advanced Hibernate V2Advanced Hibernate V2
Advanced Hibernate V2
 
Real world cross-platform testing
Real world cross-platform testingReal world cross-platform testing
Real world cross-platform testing
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with Javassist
 
The Ring programming language version 1.6 book - Part 41 of 189
The Ring programming language version 1.6 book - Part 41 of 189The Ring programming language version 1.6 book - Part 41 of 189
The Ring programming language version 1.6 book - Part 41 of 189
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
Practical Introduction to Java Memory Model
Practical Introduction to Java Memory ModelPractical Introduction to Java Memory Model
Practical Introduction to Java Memory Model
 
From C++ to Objective-C
From C++ to Objective-CFrom C++ to Objective-C
From C++ to Objective-C
 
The Ring programming language version 1.9 book - Part 47 of 210
The Ring programming language version 1.9 book - Part 47 of 210The Ring programming language version 1.9 book - Part 47 of 210
The Ring programming language version 1.9 book - Part 47 of 210
 
Cascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the StreamsCascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the Streams
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
Servletand sessiontracking
Servletand sessiontrackingServletand sessiontracking
Servletand sessiontracking
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
 

Destacado

Effective Java - Always override toString() method
Effective Java - Always override toString() methodEffective Java - Always override toString() method
Effective Java - Always override toString() method
Ferdous Mahmud Shaon
 
000 Culture rebuild start
000 Culture rebuild start000 Culture rebuild start
000 Culture rebuild start
Graylit
 
Музей Орсе
Музей ОрсеМузей Орсе
Музей Орсе
MelanieLa
 
The mind of the CEO
The mind of the CEOThe mind of the CEO
The mind of the CEO
Graylit
 
Recomendaciones eneldaada-120416050758-phpapp02
Recomendaciones eneldaada-120416050758-phpapp02Recomendaciones eneldaada-120416050758-phpapp02
Recomendaciones eneldaada-120416050758-phpapp02
Caro Conyna Cortes
 

Destacado (20)

Approaches to cDNA Cloning and Analysis
Approaches to cDNA Cloning and AnalysisApproaches to cDNA Cloning and Analysis
Approaches to cDNA Cloning and Analysis
 
Effective Java - Always override toString() method
Effective Java - Always override toString() methodEffective Java - Always override toString() method
Effective Java - Always override toString() method
 
Business Communcation
Business CommuncationBusiness Communcation
Business Communcation
 
Est Ppt
Est PptEst Ppt
Est Ppt
 
GBP AFH Quality Awareness
GBP AFH Quality AwarenessGBP AFH Quality Awareness
GBP AFH Quality Awareness
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 
Quality induction
Quality inductionQuality induction
Quality induction
 
Introduction to HTML and CSS
Introduction to HTML and CSSIntroduction to HTML and CSS
Introduction to HTML and CSS
 
La didattica della fisica nell'era digitale
La didattica della fisica nell'era digitaleLa didattica della fisica nell'era digitale
La didattica della fisica nell'era digitale
 
Honor Killings - A Facet of Sharia Law Worldwide
Honor Killings - A Facet of Sharia Law WorldwideHonor Killings - A Facet of Sharia Law Worldwide
Honor Killings - A Facet of Sharia Law Worldwide
 
280 juristas recuerdan que el Estado ha de garantizar los derechos de las per...
280 juristas recuerdan que el Estado ha de garantizar los derechos de las per...280 juristas recuerdan que el Estado ha de garantizar los derechos de las per...
280 juristas recuerdan que el Estado ha de garantizar los derechos de las per...
 
000 Culture rebuild start
000 Culture rebuild start000 Culture rebuild start
000 Culture rebuild start
 
Fundamentals of API Security Management
Fundamentals of  API Security ManagementFundamentals of  API Security Management
Fundamentals of API Security Management
 
Музей Орсе
Музей ОрсеМузей Орсе
Музей Орсе
 
8 Steps to creating a talent pool
8 Steps to creating a talent pool8 Steps to creating a talent pool
8 Steps to creating a talent pool
 
Vighnahar no a-corporate presentation
Vighnahar no a-corporate presentationVighnahar no a-corporate presentation
Vighnahar no a-corporate presentation
 
Movie script
Movie scriptMovie script
Movie script
 
The mind of the CEO
The mind of the CEOThe mind of the CEO
The mind of the CEO
 
презентация рппс комп. гр
презентация рппс комп. грпрезентация рппс комп. гр
презентация рппс комп. гр
 
Recomendaciones eneldaada-120416050758-phpapp02
Recomendaciones eneldaada-120416050758-phpapp02Recomendaciones eneldaada-120416050758-phpapp02
Recomendaciones eneldaada-120416050758-phpapp02
 

Similar a Effective Java - Override clone() method judiciously

1.what is the difference between a instance variable and an local va.pdf
1.what is the difference between a instance variable and an local va.pdf1.what is the difference between a instance variable and an local va.pdf
1.what is the difference between a instance variable and an local va.pdf
archgeetsenterprises
 

Similar a Effective Java - Override clone() method judiciously (20)

04 threads
04 threads04 threads
04 threads
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
 
Mockito intro
Mockito introMockito intro
Mockito intro
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
 
Test Engine
Test EngineTest Engine
Test Engine
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
Design patterns with kotlin
Design patterns with kotlinDesign patterns with kotlin
Design patterns with kotlin
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in Practise
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Test Engine
Test EngineTest Engine
Test Engine
 
Design patterns with Kotlin
Design patterns with KotlinDesign patterns with Kotlin
Design patterns with Kotlin
 
DeadLock Preventer
DeadLock PreventerDeadLock Preventer
DeadLock Preventer
 
Latest C++ Interview Questions and Answers
Latest C++ Interview Questions and AnswersLatest C++ Interview Questions and Answers
Latest C++ Interview Questions and Answers
 
1.what is the difference between a instance variable and an local va.pdf
1.what is the difference between a instance variable and an local va.pdf1.what is the difference between a instance variable and an local va.pdf
1.what is the difference between a instance variable and an local va.pdf
 
25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.ppt25-inheritance-polymorphism.ppt
25-inheritance-polymorphism.ppt
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
 
Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...Singletons in PHP - Why they are bad and how you can eliminate them from your...
Singletons in PHP - Why they are bad and how you can eliminate them from your...
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Último (20)

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 

Effective Java - Override clone() method judiciously

  • 1.
  • 2.
  • 3.
  • 4.
  • 5. Swimmer String name swimTime eventDate 26.31 Kristen Date year 2005 etc. 1982 Immutable mutable cu1 d
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.