What is the need to implement Serializable interface (with
no methods) for objects which are to be serialized ?
We can write our own functionality which writes objects to
streams then why we need to implement and tell JVM that
which objects can be serialized.
Answers were Sorted based on User's Feedback
Answer / chellammal
Implementing Serializable interface signifies that the
object can be persisted. Otherwise, when you try to
use writeObject, it will throw
java.io.NotSerializableException
| Is This Answer Correct ? | 15 Yes | 2 No |
Answer / rohit
Serializable concept is related with the state persisitence
of the object.
Example:---
When u send the some object on internet it needs to be
serialized bcz before sending the object it first converted
in to byte & desrerialization is the process is used to get
it back in its original form
| Is This Answer Correct ? | 6 Yes | 1 No |
Answer / akj504
Ya that answers are expected but...
If we open ObjectOutputStream.java (which is used for
serialization) then we see the following code :
............................
writeObject0(Object obj, boolean unshared){
.............
} else if (cl.isArray()) {
writeArray(obj, desc, unshared);
} else if (obj instanceof Serializable) {
writeOrdinaryObject(obj, desc, unshared);
} else {
throw new NotSerializableException(cl.getName());
}
................
If we remove the checking instanceof (& recompile) or if we
override the writeObject0 method then
Will it still check whethere the class has implemented
serilizable ?
| Is This Answer Correct ? | 1 Yes | 1 No |
Answer / dbin
The problem with your own functionality is with deserialization process , when reconstructing the new objects that are equal to the objects you are trying to save....
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / aman kr aggarwal
well if we dont use Serializable interface which is a
marker interface, then the object which we want to
serialize and use writeObject() method then compiler would
throw exception as if other object which r related to that
object if not serialized.
So. to overcome this we have to use Seializable interface.
| Is This Answer Correct ? | 3 Yes | 6 No |
What is the Dictionary class?
public class AboutStrings{ public static void main(String args[]){ String s1="hello"; String s2="hel"; String s3="lo"; String s4=s2+s3; //to know the hash codes of s1,s4. System.out.println(s1.hashCode()); System.out.println(s4.hashCode()); // these two s1 and s4 are having same hashcodes. if(s1==s4){ System.out.println("s1 and s4 are same."); }else System.out.println("s1 and s4 are not same."); } } Somebody told me that, == operator compares references of the objects. In the above example even though s1 and s4 are refering to same object(having same hash codes), it is printing s1 and s4 are not same. Can anybody explain in detail why it is behaving like this? Thanks in Advance RavuriVinod
What is locale in java?
Can you inherit a constructor java?
Explain reverse a linked list recursive java solution?
What is the use of static import ?
Explain the protected field modifier?
Why is flag used in java?
Can we override compareto method?
what is the replacement method of stop() of thread
How do you relate a Interface to a Class? Tell me in Detail?
What is garbage collection in Java, and how can it be used ?
32 Answers Accenture, HCL, Infosys, Sara, SITS, TCS, Wipro,