Class Hierarchy II Discussion E Hierarchy A mail
- Slides: 23
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 Clothing Tax
public abstract class Merchandise { Tax tax; public int get. Cost() {} public int get. Tax(int zip. Code) { return tax. get. Tax(zip. Code); } } public class Clothing extends Merchandise { public Clothing () { tax = new Clothing. Tax(this); } public int get. Cost() {} }
public abstract class Tax { Merchandise article; public Tax(); public int get. Tax(int zip. Code); } public class Clothing. Tax extends Tax { //imagine a static zipcode indexed table for looking //up taxation public Clothing. Tax(Clothing article) {this. article=article; } public int get. Tax(int zip. Code); } We may want to model zip code explicitly using a Location class.
Interfaces Merchandise Clothing Tax
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 Location MA Clothing Tax 01003
Relationships ● Is-A – ● Has-A – ● composition Uses – ● inheritence parameters, calls Is-Used-By – Uses of other classes
Ellipse and Circle b x 2 + y 2 -- -- = 1 a 2 b 2 public class Ellipse { int a; int b; } r a x 2 + y 2 =r 2 public class Circle { int r; }
Object Serialization ● ● Applications need to save data Saving an object may require saving of a subgraph 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. Object. Input. Stream, java. io. Object. Output. Stream define default methods to read and write objects
Example (2) public class X int x; } implements Serializable 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 { } File. Output. Stream fs = new File. Output. Stream("y. save"); Object. Output. Stream out = new Object. Output. Stream(fs); out. write. Object(yobj); out. close();
Example (4) public void restore { File. Input. Stream fs = new File. Input. Stream("y. save"); Object. Input. Stream in = new Object. Input. Stream(fs); yobj = (Y) in. read. Object(); in. close(); }
Safety ● Java generates a serial. Version. UID for each class ● Matched for correctness at de-serialization ● You can override by defining you own field in class – static final long serial. Version. UID = XXXX
Limitation ● No control on what gets written – Class info, fields stored as <name, value> pair ● Designed for generality ● Customization ● – read. Object(), call default. Read. Object() first – write. Object(), call default. Write. Object() 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 Object. Input Object. Output can be used
Example (5) public class X implements Externalizable { int x; void read. External(Object. Input in) throws IOEx, CNFEx{} void write. External(Object. Output out) throws IOEx {} } public class Y implements Externalizable { int y; X xobj; void read. External(Object. Input in) throws IOEx, CNFEx{} void write. External(Object. Output out) throws IOEx {} }
Example (6) public class X implements Externalizable { int x; void read. External(Object. Input in) throws IOEx, CNFEx { super. read. External(in); x = in. read. Int(); } void write. External(Object. Output out) throws IOEx { super. write. External(); out. write. Int(x); } }
Example (7) public class Y int y; X xobj; } implements Externalizable { void read. External(Object. Input in) throws IOEx, CNFEx{ super. read. External(in); y = in. read. Int(); xobj = in. read. Object(); } void write. External(Object. Output out) throws IOEx { super. write. External(); out. write. Int(y); out. write. Object(xobj); }
Example (8) public class App { Y yobj; } public void save { File. Output. Stream fs= new File. Output. Stream("y. save"); Object. Output out = new Object. Output(fs); yobj. write. External(out); out. close(); } public void restore { File. Input. Stream fs = new File. Input. Stream("y. save"); Object. Input in = new Object. Input(fs); Y nyobj = new Y(); nyobj. read. External(in); in. close(); }
- Parallelism examples
- Priority mail vs priority mail express
- Google docshttps://mail.google.com/mail/u/0/#inbox
- Fractional distillation conclusion
- Class discussion rules
- Types of exception in java
- Swing class hierarchy
- Wpf class hierarchy
- Java exception hierarchy
- Roman class hierarchy
- Haskell class hierarchy
- We are going today
- Putting a package together
- Difference between abstract class and concrete class
- Measure of central tendency of grouped data
- Class i vs class ii mhc
- Abstract concrete class relationship
- Width of class interval
- Sd and sdelta
- Stimuli vs stimulus
- 7 rights of medication administration in order
- Class maths student student1 class student string name
- What is the class width for the given class (28-33)
- In greenfoot, you can cast an actor class to a world class?