C OMP 401 C OPY : S HALLOW AND D EEP Instructor: Prasun Dewan.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Pointers Prasun Dewan Comp 114.
CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
C8: Understanding Inheritance. Intuitive description Intuitive: FLORISTS are SHOPKEEPERS, inheriting various shopkeeper behaviors Tension in OOP languages:
ITEC200 – Week03 Inheritance and Class Hierarchies.
C OMP 401 M EMORY R EPRESENTATION OF P RIMITIVE V ALUES AND O BJECTS Instructor: Prasun Dewan.
C HAPTER 8 Serious Polymorphism. O VERVIEW When to not instantiate Abstract Classes Abstract Methods Class Object Casting and Object reference Multiple.
J AVA AND V ARIABLES. O VERVIEW Declaring a Variable Primitive Types Java Keywords Reference Variables Object Declaration and Assignment Objects and Garbage.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Primitive Values Vs Objects Wrapper Classes Memory Representation of Primitive Values Vs Objects –Pointers Equal Vs == Sharing of Objects Garbage Collection.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Inheritance One of the biggest advantages of object-oriented design is that of inheritance. A class may be derived from another class, the base class.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
08 1 Abstract Data Types Problem Sets: PS2 due due Monday, Feburary 26 PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 08 Monday, February 26.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Advanced Java Programming CS 537 – Data Structures and Algorithms.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
C OMP 401 A NIMATION : MVC Instructor: Prasun Dewan.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Inheritance Type/Subtype Relationship. Inheritance Idea: An object B of one type, termed child class, inherits from another object A of another type,
Ceg860(Prasad)L8Obj1 Objects Run-time Structure and Organization.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
CS 151: Object-Oriented Design November 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java Inheritance 1/13/2015. Learning Objectives Understand how inheritance promotes software reusability Understand notions of superclasses and subclasses.
Classes Revisited Chapter 8.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
C11, Implications of Inheritance
Polymorphism 2nd Lecture
Object-oriented Programming in Java
CSC 205 Java Programming II
Object-Oriented Programming: Inheritance
Java Inheritance.
Chapter 8 Classes and Objects
Computer Science II Exam 1 Review.
Java Inheritance.
Comp 401 Metamorphosis: Casting vs. Conversion
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Parameter Passing Actual vs formal parameters
Sampath Kumar S Assistant Professor, SECE
Review Session Biggest discrepancy questions
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Java Inheritance.
Presentation transcript:

C OMP 401 C OPY : S HALLOW AND D EEP Instructor: Prasun Dewan

2 P REREQUISITE Composite Object Shapes Inheritance

3 C LONE S EMANTICS ? Object toString() equals() clone() Need to understand memory representation

4 C OPYING O BJECTS What if we want copy rather than reference. The properties can be changed independently Backup p1 = new AMutablePoint(200, 200); p2 = p1; p1.setX (100); p2.getX() == p1.getX()  true

5 C OPIER DOES THE WORK p1 = new AMutablePoint(200, 200); p2 = new AMutablePoint (p1.getX(), p1.getY()); p1.setX (100); p2.getX() == p1.getX()  false

6 C OPIED OBJECT DOES THE WORK // in Object, subtype can increase access of overridden method protected Object clone() { … } p2.getX() == p1.getX()  false // Cloneable is an empty interface, should be an annotation public class ACloneablePoint extends AMutablePoint implements CloneablePoint, Cloneable { public ACloneablePoint(int theX, int theY) { super(theX, theY); } public Object clone() { return super.clone(); } public interface CloneablePoint extends MutablePoint { public Object clone(); } CloneablePoint p1 = new ACloneablePoint(200, 200); CloneablePoint p2 = (CloneablePoint) p1.clone(); p1.setX (100);

7 B OUNDED P OINT C LONE public class ACloneableBoundedPoint extends ABoundedPoint implements CloneableBoundedPoint, Cloneable { public ACloneableBoundedPoint(int initX, int initY, CloneablePoint theUpperLeftCorner, CloneablePoint theLowerRightCorner) { super(initX, initY, theUpperLeftCorner, theLowerRightCorner); } public Object clone() { return super.clone(); }

8 B OUNDED P OINT C LONE p2.getX() == p1.getX()  false p1.getUpperLeftCorner().getX() == p2.getUpperLeftCorner().getX()  true CloneableBoundedPoint p1 = new ACloneableBoundedPoint (75, 75, new AMutablePoint(50,50), new AMutablePoint(100,100)); CloneableBoundedPoint p2 = (CloneableBoundedPoint) p1.clone(); p1.setX (100); p1.getUpperLeftCorner().setX(200);

9 R EPLICATING I NSTANCE V ARIABLE V ALUES

10 S HALLOW C OPY Pointer VariablePrimitive Variable

11 D EEP C OPY Pointer VariablePrimitive Variable

12 O BJECT C LONE // Object implements shallow copy protected Object clone() { … } //class can implement multiple interfaces, and interface such as Cloneable can be empty public class AMutablePoint implements Point, Cloneable // Subclass can make it public public Object clone() { return super.clone() } // need exception handling, discussed later

13 B OUNDED P OINT D EEP C OPY ? public Object clone() { }; public class ABoundedPoint extends AMutablePoint implements BoundedPoint { Point upperLeftCorner, lowerRightCorner; public ABoundedPoint ( int initX, int initY, Point initUpperLeftCorner, Point initLowerRightCorner) { super (initX, initY); upperLeftCorner = initUpperLeftCorner; lowerRightCorner = initLowerRightCorner; } … }

14 B OUNDED P OINT D EEP C OPY CloneableBoundedPoint p1 = new ACloneableBoundedPoint (75, 75, new ACloneablePoint(50,50), new ACloneablePoint(100,100)); CloneableBoundedPoint p2 = p1.clone(); p1.setX (100); p1.getUpperLeftCorner().setX(200); p2.getX() == p1.getX()  false p1.getUpperLeftCorner().getX() == p2.getUpperLeftCorner().getX()  false public CloneableBoundedPoint clone() { return new ACloneableBoundedPoint (x, y, (CloneablePoint) ((CloneablePoint)upperLeftCorner).clone(), (CloneablePoint) ((CloneablePoint)lowerRightCorner).clone()); }

15 B OUNDED P OINT D EEP C OPY P ROBLEMS CloneableBoundedPoint p1 = new ACloneableBoundedPoint (75, 75, new ACloneablePoint(50,50), new ACloneablePoint(100,100)); p1.setUpperLeftCorner(p1); CloneableBoundedPoint p2 = p1.clone(); Infinite recursion

16 C LONING G RAPH S TRUCTURES Graph structures are useful and make deep copy problematic Link from child to parent often occurs public CloneableBoundedPoint clone() { return new ACloneableBoundedPoint ( x, y, upperLeftCorner.clone(), lowerRightCorner.clone()); }

17 S HALLOW VS. D EEP C OPY Shallow copy: Copies the instance but not its components Creates a new object and assigns instance variables of copied object to corresponding instance variables of new object. Deep copy Creates a new object and assigns (deep or shallow?) copies of instance variables of copied object to corresponding instance variables of new object.

18 SMALLTALK S HALLOW, D EEP ( ER ), AND R EGULAR C OPY Copy Programmer makes it either shallow or deep copy. By default it is shallow. Shallow copy: Copies the instance but not its components Creates a new object and assigns instance variables of copied object to corresponding instance variables of new object. Deep copy Creates a new object and assigns copy of each instance variable of copied object to corresponding instance variable of new object.

19 D EEP C OPY OF G RAPH S TRUCTURE If copy is deepCopy

20 D EEP C OPY OF G RAPH S TRUCTURE If copy is shallowCopy

21 I SOMORPHIC D EEP C OPY

22 JAVA SERIALIZATION Used to copy object (implementing java.io.Serializable empty interface) to file or network Deep isomorphic copy Created a deep isomorphic copy of object Used by OE library to create a deepCopy public Object Misc.deepCopy(Object object) Deep copy used for automatic refresh import java.io.Serializable; public class AMutablePoint extends AMutablePoint implements Point, Serializable { … }

23 O BJECT E DITOR A UTOMATIC R EFRESHES How does OE know that the value changed? Suppose some operation on changes Y coordinate of All components of have the same value

24 O BJECT E DITOR A UTOMATIC R EFRESHES Creates an isomorphic copy when it first encounters an object Suppose an operation is executed on an object Calls equals() on new object to compare it with copy If equals returns false, efficiently updates display of changed object and creates new copy of changed object

25 C OMPLETE B RUTE F ORCE R EFRESH If object and all of its descendants not Serializable then no deep copy or efficient refresh

26 C OMPLETE B RUTE F ORCE R EFRESH If object and all of its descendants Serializable, but no overridden equals(), then complete brute force refresh Can override equals() and set break point in it to verify it is called with copy

27 W HY S IMPLISTIC A RRAY P RINT ? Object[] recursive = new Object[1]; recursive[0] = recursive; System.out.println(recursive); Infinite recursion if println() recursively printed each element

28 O THER O BJECT O PERATIONS hashCode() Relevant to hashtables – will learn about in data structures. Think of it as the internal address of object. Various versions of wait() and notify() Relevant to threads – will study them in depth in operating systems course See section on synchronization and wait and notify. Useful for animations. getClass() Returns the class, on which one can invoke “reflection” methods Used by ObjectEditor to edit arbitrary object. finalize() Called when object is garbage collected.

29 G ARBAGE C OLLECTION variablesmemoryaddress Point p1 = new AMutablePoint(100,100); 100 Point p1528 Point p256 p1 p Point p2 = new AMutablePoint(150,75); p2 = p1 ; Garbage Collected Covered elsewhere