15.
Time for some practical example <ul><li>A rudimentary banking application </li></ul><ul><li>Objective: Create some customers and create their saving and current accounts. Display them on the screen. That’s it! </li></ul>

16.
Practical Example 2 <ul><li>Duck simulation game once again? </li></ul><ul><li>Objective: To show different type of ducks on the screen (Time being each duck can be represented by different text). Some of the ducks can fly / quack while some cannot. Code should first be implemented for Mallard and rubber duck. It should be expandable for other duck type. </li></ul>

19.
Collections – some groundwork 3 <ul><li>hashCode() </li></ul><ul><li>Object ID, not necessarily unique, used my HashMap / HashSet etc to store reference to the object. hashCode is used to locate the object in the memory. </li></ul>

20.
Collections – An Introduction <ul><li>What are collections? </li></ul><ul><li>What do we do with collections? </li></ul><ul><li>Add objects </li></ul><ul><li>Remove objects </li></ul><ul><li>Find an Object </li></ul><ul><li>Iterate through collection </li></ul>

24.
Collections - Search <ul><li>Searches are performed using binarySearch() </li></ul><ul><li>Successful searches return index of element being searched </li></ul><ul><li>Collection / Array should be sorted to facilitate search </li></ul><ul><li>If searched element is not there, then the return value of search is = -(index at which if searched element is inserted, sorted order will be maintained) -1. Like a, c, d, e, f if we search for b, return value will be -2 </li></ul><ul><li>Array / Collection is sorted using comparator, same comparator should be used in search. </li></ul>

44.
Serialization <ul><li>Preserving state of object in a file </li></ul><ul><li>Called serialization or flattening of objects </li></ul><ul><li>Three ways to do Serialization: </li></ul><ul><ul><li>using the default protocol </li></ul></ul><ul><ul><li>customizing the default protocol </li></ul></ul><ul><ul><li>creating our own protocol </li></ul></ul>

45.
Serialization using the default protocol <ul><li>You have to implement interface “Serializable” </li></ul><ul><li>This interface has no methods. So it is just a marker interface. </li></ul><ul><li>ObjectOutputStream </li></ul>

46.
Serialization using Customizing the Default Protocol - I <ul><li>What will happen if our class contains objects of some other classes which are not serializable? </li></ul>

47.
Serialization using Customizing the Default Protocol - II <ul><li>We have to mark those classes as serializable? </li></ul><ul><li>What if we don’t have access to code of those classes? </li></ul><ul><ul><li>In case, it is not important to save state of those objects, the way is to mark the variables referring to those objects as transient. </li></ul></ul><ul><ul><li>But, what if we also want to save state of object. </li></ul></ul>

48.
Serialization using Customizing the Default Protocol - III <ul><li>So, What should we do to instantiate transient variables? Call our own method after de-serialization? </li></ul><ul><li>Of course this is a solution. But, what if the other developers does not know about that? </li></ul><ul><li>What should we do now? </li></ul>

49.
Serialization using Customizing the Default Protocol - II <ul><li>Provide following two methods in your class: </li></ul><ul><ul><li>private void writeObject(ObjectOutputStream out) throws IOException; </li></ul></ul><ul><ul><li>private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException; </li></ul></ul><ul><ul><li>Do your operations and then call: </li></ul></ul><ul><ul><li>defaultWriteObject(); </li></ul></ul><ul><ul><li>defaultReadObject(); </li></ul></ul><ul><ul><li>Note that these methods are private. So they are not inhereted, overloaded. In fact, when VM sees that class provides implementation of these methods it call these methods than default methods. Remember, VM can call private methods, any other oject cannot. </li></ul></ul><ul><ul><li>Also, they can be used to make a call non-serializable even if super class is serializable. </li></ul></ul>

50.
Serialization using Creating our Own Protocol <ul><li>Instead of implementing the Serializable interface, you can implement Externalizable, which contains two methods: </li></ul><ul><ul><li>public void writeExternal(ObjectOutput out) throws IOException; </li></ul></ul><ul><ul><li>public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException; </li></ul></ul><ul><li>Just override those methods to provide your own protocol. This protocol is entirely in your hands. An example situation for that alternate type of serialization: read and write PDF files with a Java application. If you know how to write and read PDF (the sequence of bytes required), you could provide the PDF-specific protocol in the writeExternal and readExternal methods. </li></ul>