class hierarchy ii
DESCRIPTION
Discussion E. Class Hierarchy II. Hierarchy. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/1.jpg)
Class Hierarchy II
Discussion E
![Page 2: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/2.jpg)
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.
![Page 3: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/3.jpg)
Merchandise
Merchandise Tax
Clothing Clothing Tax
![Page 4: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/4.jpg)
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() {}}
![Page 5: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/5.jpg)
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.
![Page 6: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/6.jpg)
Interfaces
Merchandise Tax
Clothing
![Page 7: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/7.jpg)
Extension
● assumed that tax rate was flat for a type● it may depend on cost of item
– clothes > $250 may be taxed differently
![Page 8: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/8.jpg)
Detailed
Merchandise Tax
Clothing Clothing Tax
Location
01003
MA
![Page 9: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/9.jpg)
Relationships
● Is-A– inheritence
● Has-A– composition
● Uses– parameters, calls
● Is-Used-By– Uses of other classes
![Page 10: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/10.jpg)
Ellipse and Circle
ab r
public class Ellipse { int a; int b;}
public class Circle { int r;
}
x2 + y2 =r2
x2 + y2
-- -- = 1a2 b2
![Page 11: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/11.jpg)
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
![Page 12: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/12.jpg)
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
![Page 13: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/13.jpg)
Serialization
● java.io.Serializable● Marker interface, has no methods● java.io.ObjectInputStream, java.io.ObjectOutputStream define default methods to read and write objects
![Page 14: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/14.jpg)
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() {}
}
![Page 15: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/15.jpg)
Example (3)
public void save {
FileOutputStream fs = new FileOutputStream("y.save"); ObjectOutputStream out = new ObjectOutputStream(fs); out.writeObject(yobj); out.close();
}
![Page 16: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/16.jpg)
Example (4)
public void restore {
FileInputStream fs = new FileInputStream("y.save"); ObjectInputStream in = new ObjectInputStream(fs); yobj = (Y) in.readObject(); in.close();
}
![Page 17: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/17.jpg)
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
![Page 18: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/18.jpg)
Limitation● No control on what gets written
– Class info, fields stored as <name, value> pair
● Designed for generality● Customization
– readObject(), call defaultReadObject() first– writeObject(), call defaultWriteObject() first
● Optimization: fields marked transient are not serialized
![Page 19: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/19.jpg)
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
![Page 20: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/20.jpg)
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 {}}
![Page 21: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/21.jpg)
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); }}
![Page 22: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/22.jpg)
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); }}
![Page 23: Class Hierarchy II](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681571b550346895dc4badb/html5/thumbnails/23.jpg)
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(); } }