Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Serialiazble interface

 
Prakash Pillai
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Graeme Brown
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jorge Phillips
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic