This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Hi guys, I am curious to know . 1.How Serializable interface works.As dosn't have any method to implement ? 2. Serializable s=new String("Hello"); How the above statement works or gets complied,as String is class and Serializable is interface ? Thanx, Prakash
1. Serializable is a marker to the virtual machine to show that instances of implementing classes may be serialized. There is no new behaviour that you need to implement to achieve this, under normal cirumstances, so no need for Serializable to define any new methods. 2. This is to do with inheritance and polymorphism. The full definition of class String is:
this means that String is of class String but also has type of Object, Serializable and Comparable. Therefore you can provide an instance of a String any where that you need either a String, an Object, a Serializable, or a Comparable. An informal way of checking this is by the "is a" test. If you can say String is a Serializable then it is OK. For more info read How my Dog learned Polymorphism
As Graeme well puts it, a String implements the Serializable interface. When you do Serializable s = new String("bar"); you can invoke any methods on s that are declared in the Serializable interface, but String methods would be hidden. Likewise say if s is of type Object.