Class Hierarchy II Discussion E. Hierarchy A mail order business sells catalog merchandise all over the country. The rules for taxation on the merchandise.

Slides:



Advertisements
Similar presentations
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. 2 Operating System Application #1 Application #2 Java Virtual Machine #1 Local Memory Shared Memory Threads.
Advertisements

Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
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.
CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces.
Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
COP INTERMEDIATE JAVA Exception Handling Serialization.
1 Chapter 8 Three Interfaces: Cloneable, Serializable, and Runnable.
Object Serialization in Java Or: The Persistence of Memory…
File-based Persistence: Serializability COMP53 Dec 7, 2007.
Java Iterators interface Collection { … Iterator iterator(); Iterator iterator(); …} interface Set extends Collection { … Iterator iterator(); Iterator.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
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,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Class Hierarchy Discussion D. Constructor public class X { private int capacity; public X() { capacity = 16;} public X(int i) {capacity = i;} public int.
Files and Streams (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng
CIS 270—Application Development II Chapter 14—Files and Streams.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Object Serialization in Java Or: The Persistence of Memory… Originally from:
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.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
Inheritance and Access Control CS 162 (Summer 2009)
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Outline  Basic IO  File class  The Stream Zoo  Serialization  Regular Expressions.
Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Serialization. Sequential-access Text Files Sequential-access files store records In order by the record-key field Java imposes no structure on.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Binary I/O.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
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.
COSC 1P02 Data Structures and Abstraction 2.1 Cosc 1P02 Binary Files & Persistent Objects “For myself, I am an optimist--it does not seem to be much use.
Chapter 11 Exceptions and Input/Output Operations.
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.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
1 Copyright © 2011 Tata Consultancy Services Limited TCS Internal.
Files and Serialization. Files Used to transfer data to and from secondary storage.
CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1.
File Input & Output1 Streams & File I/O. Introduction (1) The Java platform includes a number of packages that are concerned with the movement of data.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Graphical User Interfaces [notes Chap 7] and [AJ Chap 17, Sec 13.2] 1.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
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
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
University Of Karachi Department Of Computer Science
Chapter 8: Input and Output
Chapter 17 Binary I/O.
Object Writing in files
Chapter 17 Binary I/O 1.
Accessing Files in Java
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
Behavioral and Structural Patterns
Persistence & Bean Properties
null, true, and false are also reserved.
Interface.
Interfaces.
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
Command Invocation Protocol
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
Podcast Ch23f Title: Serialization
The command invocation protocol
OO Java Programming Input Output.
Presentation transcript:

Class Hierarchy II Discussion E

Hierarchy A mail order business sells catalog merchandise all over the country. The rules for taxation on the merchandise vary from state to state. Also, the rate of taxation can be different based on the type of merchandise. For simplicity, we will consider three types of merchandise, clothing, pharmaceutical, and beauty products. Suggest a class hierarchy to model the tax on a merchandise.

Merchandise Tax Clothing Clothing Tax

public abstract class Merchandise { Tax tax; public int getCost() {} public int getTax(int zipCode) { return tax.getTax(zipCode); } public class Clothing extends Merchandise { public Clothing () { tax = new ClothingTax(this); } public int getCost() {} }

public abstract class Tax { Merchandise article; public Tax(); public int getTax(int zipCode); } public class ClothingTax extends Tax { //imagine a static zipcode indexed table for looking //up taxation public ClothingTax(Clothing article) {this.article=article;} public int getTax(int zipCode); } We may want to model zip code explicitly using a Location class.

Interfaces Merchandise Tax Clothing

Extension ● assumed that tax rate was flat for a type ● it may depend on cost of item – clothes > $250 may be taxed differently

Detailed Merchandise Tax Clothing Clothing Tax Location MA

Relationships ● Is-A – inheritence ● Has-A – composition ● Uses – parameters, calls ● Is-Used-By – Uses of other classes

Ellipse and Circle a b r public class Ellipse { int a; int b; } public class Circle { int r; } x 2 + y 2 =r 2 x 2 + y = 1 a 2 b 2

Object Serialization ● Applications need to save data ● Saving an object may require saving of a sub- graph ● Object graph converted in series of bytes ● De-Serialization recreates objects from these bytes

Example (1) public class X { int x; } public class Y { int y; X xobj; } public class App { Y yobj; } x y Y: y X: x

Serialization ● java.io.Serializable ● Marker interface, has no methods ● java.io.ObjectInputStream, java.io.ObjectOutputStream define default methods to read and write objects

Example (2) public class X implements Serializable { int x; } public class Y implements Serializable { int y; X xobj; } public class App { Y yobj; public void save() {} public void restore() {} }

Example (3) public void save { FileOutputStream fs = new FileOutputStream("y.save"); ObjectOutputStream out = new ObjectOutputStream(fs); out.writeObject(yobj); out.close(); }

Example (4) public void restore { FileInputStream fs = new FileInputStream("y.save"); ObjectInputStream in = new ObjectInputStream(fs); yobj = (Y) in.readObject(); in.close(); }

Safety ● Java generates a serialVersionUID for each class ● Matched for correctness at de-serialization ● You can override by defining you own field in class – static final long serialVersionUID = XXXX

Limitation ● No control on what gets written – Class info, fields stored as pair ● Designed for generality ● Customization – readObject(), call defaultReadObject() first – writeObject(), call defaultWriteObject() first ● Optimization: fields marked transient are not serialized

Externalizable ● Lightweight – does not store name-value pair – order is important, serialization allows reading in any order – explicitly handle class hierarchy – no argument constructor needed ● Allows complete control on what gets written ● Methods defined in ObjectInput ObjectOutput can be used

Example (5) public class X implements Externalizable { int x; void readExternal(ObjectInput in) throws IOEx,CNFEx{} void writeExternal(ObjectOutput out) throws IOEx {} } public class Y implements Externalizable { int y; X xobj; void readExternal(ObjectInput in) throws IOEx,CNFEx{} void writeExternal(ObjectOutput out) throws IOEx {} }

Example (6) public class X implements Externalizable { int x; void readExternal(ObjectInput in) throws IOEx, CNFEx { super.readExternal(in); x = in.readInt(); } void writeExternal(ObjectOutput out) throws IOEx { super.writeExternal(); out.writeInt(x); }

Example (7) public class Y implements Externalizable { int y; X xobj; void readExternal(ObjectInput in) throws IOEx, CNFEx{ super.readExternal(in); y = in.readInt(); xobj = in.readObject(); } void writeExternal(ObjectOutput out) throws IOEx { super.writeExternal(); out.writeInt(y); out.writeObject(xobj); }

Example (8) public class App { Y yobj; public void save { FileOutputStream fs= new FileOutputStream("y.save"); ObjectOutput out = new ObjectOutput(fs); yobj.writeExternal(out); out.close(); } public void restore { FileInputStream fs = new FileInputStream("y.save"); ObjectInput in = new ObjectInput(fs); Y nyobj = new Y(); nyobj.readExternal(in); in.close(); }