CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Serialization Flatten your object for automated storage or network.
Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
Written by: Dr. JJ Shepherd
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Chapter 8 Three Interfaces: Cloneable, Serializable, and Runnable.
Java Exceptions, Cloning, Serialization Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
Mutable, Immutable, and Cloneable Objects Chapter 15.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
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.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
1 Java object model Part 3: Serialization & Reflection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
Windows Programming Using C# Windows Services, Serialization, and Isolated Storage.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 TCSS 360, Spring 2005 Lecture Notes Design Patterns: Singleton, Memento, Flyweight.
CSC 243 – Java Programming, Spring 2013 March 12, 2013 Week 7, Generics.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java I/O Writing and Reading Objects to File Serialization.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
CHAPTER 5 ArrayLists.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Does not implement clone() method! public class Bar { … public Object clone() { … } Does not implement Cloneable interface!
Lecture 19 Serialization Richard Gesick. Serialization Sometimes it is easier to read or write entire objects than to read and write individual fields.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Chapter 19 Binary I/O. 2 Motivations F Data stored in a text file – is represented in human-readable form –Text file –Readable –Java source programs.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
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.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Final Review James Atlas August 12, James Atlas - CISC3702 “Family Feud” style Each team gets a chance to pick an answer Each team gets a chance.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
C OMP 401 C OPY : S HALLOW AND D EEP Instructor: Prasun Dewan.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
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.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
CSC 243 – Java Programming, Spring 2014
Object Writing in files
Chapter 8 Classes and Objects
CSE 331 Cloning objects slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Lecture 4: Interface Design
Behavioral and Structural Patterns
CSE 1030: Data Structure Mark Shtern.
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
CSC 220 – Processing Spring, 2017
CMPE212 – Reminders Assignment 2 due next Friday.
OO Java Programming Input Output.
Presentation transcript:

CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces

java.lang.Cloneable Cloning is akin to a copy constructor in C++. java.lang.Object defines this method protected Object clone() throws CloneNotSupportedException java.lang.Cloneable is a marker interface It does not define any method. A class that inherits Cloneable guarantees that it provides a public clone() method that returns a copy of the object on which clone() is invoked.

Shallow versus deep copy Shallow copy clones the elements of a container by copying references to contained objects. It does not clone contained objects. LinkedList dictionary = LIST WITH ELEMENTS LinkedList listcopy = dictionary.clone(); Deep copy clones the elements, recursively. LinkedList original = SOME ELEMENTS LinkedList duplicate = new LinkedList (); for (IFillWord game : original) { » duplicate.add(game.clone()); }

Covariance of subclass & return type Interfaces and classes that subclass Cloneable can refine its return type to a subclass of the base class clone() method, and loosen protection to public. Object has protected Object clone() IFillWord has public IFillWord clone() FillWordBasic has public FillWordBasic clone() FillWordGrows has public FillWordGrows clone() Internally clone() constructs a new object and copies fields from the original object using shallow or deep copy. Our classes use deep copy on the board and random number generator.

java.io.Serializable Serializable is also a marker interface. It guarantees that its object fields consist of primitive types, Serializable objects, and arrays of these two types. Serialization ignores fields marked as transient. A Serializable object easily converts to and from a byte stream (a byte []) for transmission over a network or storage in a file. There are special methods you can define for Serializable objects with some non-Serializable fields. We will ignore these for now.

Serializable Object graphs When multiple objects contain fields that point to each other, that is an object graph. Java input-output classes have automatic means for serializing an entire object graph, as long as it consists strictly of Serializable objects, primitive types, and arrays of these. Transmitting or storing object graphs is almost easy. Field private static final long serialVersionUID = A_LONG_VALUE ; is used to tag the version number of a Serialized object, in case a later version of its class changes field types.

FillWordHelper fields in Assignment 3 protected transient List dictionary protected char [][] board protected int wordSizeLimit protected RandomCloneable randomNumGenerator protected boolean isacross protected String wordToPlay protected int score protected int swaps We will clone() using deep copy on randomNumGenerator & board. Random is Serializable. I have subclassed it in RandomCloneable to make randomNumGenerator Cloneable. We will serialize every field except dictionary. We do not want to store or transmit a copy of the dictionary on every step.

Applications for us We will use serialization & deserialization to save & restore games in files in Assignment 3. We could use clone() to maintain an undo – redo stack of copies of games. We have used clone() in a past grad O-O course to allow artificial software players (AIs) to play thousands of games in searching for best moves by cloning ScrabbleGame state.