What is there in Serliazable Inteface ,as i know it is only a marker interface.Can any body speak more on this.
import java.io.Serializable;
public class Q1
{
public static void main(String[] arg)
{
Serializable se;
int []a={1,2,3};
se=a;
System.out.println("Se"+se);
}
}
Why this works ?
-
Serliazable Interface !! (2 messages)
- Posted by: Amit Gupta
- Posted on: February 06 2003 00:19 EST
Threaded Messages (2)
- Serliazable Interface !! by Lasse Koskela on February 06 2003 02:35 EST
- Serliazable Interface !! by Pranav Soni on February 06 2003 16:10 EST
-
Serliazable Interface !![ Go to top ]
- Posted by: Lasse Koskela
- Posted on: February 06 2003 02:35 EST
- in response to Amit Gupta
The Java Language Specification (chapter 10, Arrays) says
"...any array object can be assigned to any variable of these [Object, Cloneable, Serializable] types."
Check out this demonstration code:
/**
* A demonstration of arrays being Objects that implement Serializable.
*/
public class ArrayTest
{
/**
* The main method driving the test.
*/
public static void main(String[] args)
{
int[] intArray = { 1, 2, 3 };
String[] stringArray = { "a", "b", "c" };
NotSerializable[] notserArray = {
new NotSerializable(),
new NotSerializable(),
new NotSerializable()
};
java.util.HashMap[] hashmapArray = {
new java.util.HashMap(),
new java.util.HashMap(),
new java.util.HashMap()
};
printInterfaces(intArray);
printInterfaces(stringArray);
printInterfaces(notserArray);
printInterfaces(hashmapArray);
}
/**
* Prints the interfaces implemented by the given Object.
*/
private static void printInterfaces(Object obj)
{
Class[] interfaces = obj.getClass().getInterfaces();
System.out.println("\nInterfaces implemented by " + obj.getClass().getName());
for (int i=0; i < interfaces.length; i++)
{
System.out.println(" " + interfaces[i].getName());
}
}
/**
* This class does NOT implement Serializable.
*/
static class NotSerializable
{
public NotSerializable()
{
}
}
} -
Serliazable Interface !![ Go to top ]
- Posted by: Pranav Soni
- Posted on: February 06 2003 16:10 EST
- in response to Amit Gupta
(1) Array has Object as it direct superclass, it implements Cloneable and Serializable. So any Array object can be assigned to type - Object, Cloneable, Serializable.
Serializable se;
int []a={1,2,3};
se=a;
(2) System.out.println("Se"+se), call se.toString(). Question arise here is since Serializable is marker interface without method declaration, how se.toString() can be called ? Reason is - if an interface has no direct superinterfaces, then it implicitly declares public abstract method corresponding to each public instance method declared in class Object, unless it explicitly declare the method.