Java Coding 6_part3 David Davenport Computer Eng. Dept.,

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
CS102--Object Oriented Programming Lecture 3: – Defining classes (10 questions) – The StringTokenizer class – The Math class Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Robo David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to the engineering.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
From Algorithms to Architecture David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Syntax for Variables & Constants Input,
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
From Algorithms to Architecture David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding 2 David Davenport Computer Eng. Dept.,
Java Coding Extra David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 3 David Davenport Computer Eng. Dept.,
Robo – Software Engineering
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept.,
Java Coding 5 David Davenport Computer Eng. Dept.,
Chapter 13 Abstract Classes and Interfaces
By Pius Nyaanga Momanyi
Memberwise Assignment / Initialization
Inheritance Basics Programming with Inheritance
Java Coding 4 David Davenport Computer Eng. Dept.,
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Computer Programming with JAVA
© A+ Computer Science - OOP © A+ Computer Science -
Defining Classes and Methods
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Java Classes and Objects 4th Lecture
Java Coding 8 David Davenport Computer Eng. Dept.,
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Dr. R Z Khan Handout-3 Classes
Making a copy of an object: shallow and deep copy
Classes and Objects Object Creation
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
SPL – PS3 C++ Classes.
Inequalities – Identifying – Foundation – GCSE Questions
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

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!