Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces."— Presentation transcript:

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

2 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.

3 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()); }

4 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.

5 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.

6 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.

7 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.

8 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.


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

Similar presentations


Ads by Google