Mutable, Immutable, and Cloneable Objects Chapter 15.

Slides:



Advertisements
Similar presentations
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Advertisements

 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Chapter 15: Generic Methods, Classes, and Array-Based Lists
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
ITEC200 – Week03 Inheritance and Class Hierarchies.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Tirgul 1 Today’s subjects: –First programming exercise. –Java reminder & completions : reference data types, cloning, inner classes, packages. –Short reminder.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 4 Linked Lists Anshuman Razdan Div of Computing Studies
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 10 Classes Continued
Interface. Interface interface the public methods of the classWhen you talk about the interface of a class you typically mean the public methods of the.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Abstract and Nested Classes
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
Does not implement clone() method! public class Bar { … public Object clone() { … } Does not implement Cloneable interface!
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
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.
CS 61B Data Structures and Programming Methodology July 8, 2008 David Sun.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 “A picture is.
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Sorted Lists Chapter Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
3/11/2016ITK 2751 String String class objects are immutable public String s1,s2; s1 = "This is a String!!"; s2 = s1; s1:FFFF218C This is a String!! s2:FFFF218C.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 7: Cloning and RTTI
Polymorphism 2nd Lecture
Encapsulation and Java Interface
CSC 205 Java Programming II
Chapter 8 Classes and Objects
Interfaces and Inner Classes
Implementations of the ADT Stack
Computer Science II Exam 1 Review.
CSE 331 Cloning objects slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Chapter 12 Sorted Lists and their Implementations
Week 6 Object-Oriented Programming (2): Polymorphism
CSE 1030: Data Structure Mark Shtern.
Slides by Steve Armstrong LeTourneau University Longview, TX
Implementing Classes Chapter 3.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Presentation transcript:

Mutable, Immutable, and Cloneable Objects Chapter 15

2 Chapter Contents Mutable and Immutable Objects Companion Classes Using Inheritance to Form Companion Classes Cloneable Objects A Sorted List of Clones Cloning an Array Cloning a Chain

3 Mutable and Immutable Objects A mutable object belongs to a class that has mutator or set methods for its data fields The client uses set methods to change values of the object's data fields Fig An object and its reference variable chris

4 Mutable and Immutable Objects Fig An object in the list nameList (a) initially; (b) after the reference variable chris is used to change it Done by executing chris.setLast ("Smith");

5 Mutable and Immutable Objects Immutable object belongs to a class that does NOT have mutator or set methods Class said to be read only Placing immutable objects in a sorted list is a way to prevent the client from destroying the order of the list Use an immutable object if it will be shared Use a mutable object if its data will change frequently

6 Companion Classes If it is necessary to alter an immutable object Can be accomplished by having a companion class of corresponding mutable objects Also helpful to have methods that convert an object from one type to another

7 Companion Classes Fig the classes Name and ImmutableName

8 Companion Classes Java's String class is a read-only class Instances of String are immutable Java provides a companion class, StringBuffer Has a constructor that takes an instance of String as an argument Has the toString method that converts a mutable instance of StringBuffer to an immutable instance of String

9 Companion Classes Inheritance can be used to form companion classes Text shows declaration of ImmutableName Then uses this to declare the derived class Name Invokes protected methods of base class Adds mutator methods It is best to derive the mutable class from the immutable class

10 Companion Classes Fig The class Name is derived from the class ImmutableName

11 Cloneable Objects A clone is a copy of an object The Object class contains a protected method clone that returns a copy of an object The implementation of any method can invoke clone Clients cannot invoke clone unless the class overrides it, declares it public public class MyClass implements Cloneable {...

12 Cloneable Objects Fig (a) A shallow clone; (b) a deep clone.

13 Cloneable Objects Fig An instance of Name and its shallow clone.

14 Cloneable Objects Fig A clone after one of its data fields is changed.

15 Cloneable Objects A clone method for class Student that does a deep copy public Object clone() {try {Student theCopy = (Student)super.clone(); theCopy.fullName = (Name)fullName.clone(); return theCopy; } catch (CloneNotSupportedException e) {throw new Error(e.toString()); } } // end clone

16 Cloneable Objects Fig An instance of Student and its clone, including a deep copy of fullName.

17 Cloneable Objects Fig A shallow copy of fullName.

18 Tasks for a clone Method Invoke the clone method of the superclass with super.clone() Enclose call to clone in a try block Write a catch block to handle exception of CloneNotSupportedException Skip if super.clone() invokes a public clone method Clone mutable data fields of object super.clone() returned, when possible Return the clone

19 A Sorted List of Clones Recall problem of placing mutable objects in an ADT (such as a sorted list) If object is cloned before it is added to an ADT Client could access/change the ADT's data only by using ADT operations Requires that object added to the ADT be Cloneable

20 A Sorted List of Clones Fig An ADT and its client after the clone of an object is added to the ADT.

21 A Sorted List of Clones Fig The effect of getEntry if it did not return a clone.

22 A Sorted List of Clones Fig The effect of getEntry when it does return a clone.

23 Cloning an Array To make a deep clone of an array a of cloneable objects The class must implement Cloneable Invoke a.clone () Clone each object in the array Thing[ ] clonedArray = (Thing[ ])myArray.clone(); for (int index = 0; index < myArray.length; index++) clonedArray[index] = (Thing)myArray[index].clone();

24 Cloning a Chain The ADT list class must implement the interface Cloneable public class LList implements ListInterface, Cloneable { private Node firstNode; // reference to first node private int length; // number of entries in list...

25 Cloning a Chain Fig A list that stores its data in a linked chain and its shallow clone.

26 Cloning a Chain Fig A list that stores its data in a linked chain and its deep clone.