Objects and Serialization

Similar documents
Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

Objects and Iterators

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

What is Serialization?

Object Oriented Programming. What is this Object? Using the Object s Slots

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

Abstract Classes and Interfaces

Inheritance and Interfaces

CN208 Introduction to Computer Programming

CS108, Stanford Handout #12. Inheritance 2

Hashing. Reading: L&C 17.1, 17.3 Eck Programming Course CL I

List ADT. Announcements. The List interface. Implementing the List ADT

Implications of Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013

CS 231 Data Structures and Algorithms, Fall 2016

Java Magistère BFA

Definition of DJ (Diminished Java)

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

CS193j, Stanford Handout #25. Exceptions

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012

Tecniche di Progettazione: Design Patterns

School of Informatics, University of Edinburgh

Job Migration. Job Migration

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

Active Learning: Streams

Introduction to Programming Using Java (98-388)

The class Object. Lecture CS1122 Summer 2008

LARA TECHNOLOGIES EXAM_SPECIALSIX SECTION-1

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Certification In Java Language Course Course Content

File IO. Binary Files. Reading and Writing Binary Files. Writing Objects to files. Reading Objects from files. Unit 20 1

Rules and syntax for inheritance. The boring stuff

Java Enum Java, winter semester ,2018 1

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Computer Science II (20082) Week 1: Review and Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CMSC 132: Object-Oriented Programming II

CS108, Stanford Handout #8. Java Generics

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

104. Intermediate Java Programming

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

CSE 331 Lecture 4. Comparing objects; cloning

The Object clone() Method

Lecture 7. File Processing

Java Programming. Atul Prakash

Tecniche di Progettazione: Design Patterns

Introduction to Computer Science II (CSI 1101) Midterm Examination

Building Java Programs. Inheritance and Polymorphism

Chapter 17 Binary I/O. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

2018/2/5 话费券企业客户接入文档 语雀

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Self-test Java Programming

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Chapter 13 Abstract Classes and Interfaces

1 Shyam sir JAVA Notes

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or

Practice exam for CMSC131-04, Fall 2017

Cloning Enums. Cloning and Enums BIU OOP

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Checked and Unchecked Exceptions in Java

CS 2113 Software Engineering

Midterm Exam 2 CS 455, Spring 2015

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Exercises Software Development I. 08 Objects II. Generating and Releasing Objects (Constructors/Destructors, this, Object cloning) December 3rd, 2014

Advanced Programming Methods. Seminar 12

COMP 110/L Lecture 24. Kyle Dewey

CS 251 Intermediate Programming Inheritance

CSE 401/M501 Compilers

CS193k, Stanford Handout #12. Threads 4 / RMI

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

The Java Type System (continued)

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Java Persistence API (JPA) Entities

Super-Classes and sub-classes

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

Chapter 3: A Larger Example: SocketChat

Bugs in software. Using Static Analysis to Find Bugs. David Hovemeyer

Java Programming Lecture 9

CS106A, Stanford Handout #18. Writing a Class

AP CS Unit 6: Inheritance Notes

3. Java - Language Constructs I

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

15CS45 : OBJECT ORIENTED CONCEPTS

Object Oriented Design with UML and Java. PART VIII: Java IO

copy.dept_change( CSE ); // Original Objects also changed

FILE I/O IN JAVA. Prof. Chris Jermaine

Software Construction

Voyager Interoperability Guide Version 1.1 for Voyager 8.0

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Chapter 10. IO Streams

Generics method and class definitions which involve type parameters.

The Java Collections Framework and Lists in Java Parts 1 & 2

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Inheritance (Part 5) Odds and ends

Transcription:

CS193j, Stanford Handout #18 Summer, 2003 Manu Kumar Objects and Serialization Equals boolean equals(object other) vs == For objects, a == b tests if a and b are the same pointer "shallow" semantics boolean Object.equals(Object other) defined up in the Object class, does a (this == other) test -- still shallow semantics Override A class may override equals() to provide "deep" comparison semantics -- do the two objects represent the same state? e.g. String overrides equals() Calling equals() { String a = "hello"; String b = "hello"; (a == b) ==> false (a.equals(b)) ==> true equals() strategy boolean equals(object other) {... Take Object argument, return boolean -- must have the exact same prototype as the version up in Object for overriding to work. Return true on (this == other) Use (other instanceof Foo) to test class of other -- false if not same class (instanceof returns false on null ptrs) Otherwise do a field by field comparison of this and other Student equals() // in Student class... boolean equals(object obj) { if (obj == this) return(true); if (!(obj instanceof Student)) return(false); Student other = (Student)obj; return(other.units == units)

2 Clone Goal: create a "copy" of an object Given "foo" obj of class Foo, copy = foo.clone(). Copy has same state as foo, but its own memory. Probably foo.equals(copy) Cloneable interface Used as a marker that the class implements the clone() message Not compiler enforced Object.clone() is pre-built to (a) create a new instance of the right class, and (b) assign all the fields over with '=' semantics. Object.clone() gives this default behavior if the class implements the Cloneable interface. Otherwise it throws an exception. Implementing Clone Implement the Cloneable interface 1. copy = (Class) super.clone() first (must use try/catch in case the clone() fails) 2. copy the fields where a simple '=' is not deep enough Alternatives Copy Constructor -- Foo(Foo x) -- construct a new instance of Foo, based on the state of an existing foo. "Factory" method -- static method that makes new instances. May use ctors internally... static Foo Foo.newInstance(Foo x) Advantage: simpler than clone(). Does not require extra concepts, since we already understand ctors. Disadvantage: the client must know the class of the object. In contrast, a client can send x.clone() and get a copy without knowing the precise class of x. Eq Code Example // Eq.java /* Demonstrates a simple class that defines equals and clone. public class Eq implements Cloneable { private int a; private int[] values; public Eq(int init) { a = init; values = new int[10]; /* Does a "deep" compare of this vs. the other object. public boolean equals(object other) { if (other == this) return(true); if (!(other instanceof Eq)) return(false); Eq e = (Eq) other;

3 // now test if this vs. e if (a!= e.a) return(false); if (values.length!= e.values.length) return(false); for (int i=0; i<values.length; i++) { if (values[i]!= e.values[i]) return(false); return(true); /* Returns a deep copy of the object. public Object clone() { try { // first, this creats the new memory and does '=' on all fields Eq copy = (Eq)super.clone(); // copy the array over -- arrays respond to clone() themselves copy.values = (int[]) values.clone(); return(copy); catch (CloneNotSupportedException e) { return(null); public static void main(string[] args) { Eq x = new Eq(1); Eq y = new Eq(2); Eq z = (Eq) x.clone(); System.out.println("x == z" + (x==z)); // false System.out.println("x.equals(z)" + (x.equals(z))); // true Serializing Boring object <-> file problem Serialization -- somewhat automatic Serializable interface To write out an object ObjectOutputStream out; out.writeobject(obj); To read that object back in ObjectInputStream in; obj = in.readobject(); Same type When reading, cast back to what it was when written Serialization / Archiving State in memory -- objects Write objects to streamed state

4 To a disk file, or across the network, or to the system clipboard The notion of "address space" does not hold in the streamed form -- there are no pointers. Read Read the streamed form, and re-create the object in memory Synonyms Flattening Streaming Dehydrate (Rehydrate = read) Archiving 106a Memory<->Disk Translate back and forth by hand Typically use an ASCII text format Custom arrangement between your data structures and some ASCII format for reading and writing. Java Automatic Serialization Serializable interface By implementing this interface, a class declares that it is willing to be read/written by the automatic serialization machinery. Automatic Writing Automatically writes out fields inside an object The system knows how to recursively write out the state of an object Recursively follows pointers and writes those objects out too Built-Ins Most built ins know how to serialize: int, array, Point,... "transient" fields -- do not serialize Use to prevent the serialization from recurring down a branch you do not want written to disk. Comes back as null after reading. Override: readobject(), writeobject() -- to put in more customized reading/writing Versioning serialization can detect version changes when reading and refuse to read if the code for a written out object has changed since it was written out. Programmer can control this. ObjectOutputStream out; out.writeobject(obj) This one line calls the automatic serialization machinery to write out everything rooted at the given object. Classes Each written object will be identified by its class -- the reading code will need those same classes to read the stream. Array

5 For a collection of things, it may be easier to cast the whole thing into a single array that can be written in one operation. Transient Fields should be declared transient if they should not be written. They will read back in as null. ObjectInputStream in; in.readobject() CT type Read back with the same CT type it was written (Object[] or DShapeModel[]) Class If a class was written that is not present at read-time, there will be an error. If the class has the same name but a changed implementation there will be an error. It's safest to serialize classes that are stable everywhere such as Array and Point Do not change the structure of DShapeModel, or you will not be able to open old files Circularity : Solved The automatic machinery takes care of the case where the pointer graph of objects being written out has pointers in to itself. The read operation correctly re-creates the pointer graph in memory. (yay!) Dots example... Our earlier Dots example had an ArrayList of Point objects. Here's how it would write itself out... /** Given a file, write the data model to it with Java serialization. Makes an Point[] array of points and writes it which avoids the bother of iteration. (We use an array instead of the ArrayList to avoid requiring a 1.2 VM to read the file, although maybe the ArrayList would have been fine.) public void saveserial(file file) { try { ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(file)); // Use the standard collection -> array util // (the Point[0] tells it what type of array to return) Point[] points = (Point[]) dots.toarray(new Point[0]); out.writeobject(points); // serialization! out.close(); // polite to close on the way out setdirty(false); catch (Exception e) { e.printstacktrace();

6 /** Inverse of saveserial. Reads an Point[] array of Points, and adds them to our data model. private void loadserial(file file) { try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); // Read in the object -- the CT type should be exactly as it was written // -- Point[] in this case. // Transient fields would be null. Point[] points = (Point[])in.readObject(); for (int i=0; i<points.length; i++) { dots.add(points[i]); in.close(); // polite to close on the way out setdirty(false); catch (Exception e) { e.printstacktrace(); 193j Classes For hw2, we have wrapper classes that shield you from the exceptions, but otherwise behave like ObjectOutputStream and ObjectInputStream SimpleObjectWriter w; SimpleObjectWriter w = SimpleObjectWriter.openFileForWriting(filename); w.writeobject( <object>) -- write an array or object (Point[] in above example) w.close() SimpleObjectReader r; SimpleObjectReader r = SimpleObjectReader.openFileForReading(filename); obj = r.readobject() -- returns the object written -- cast to what it is (Point [] in above example) r.close()