Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt abt Serializable,Set,Cloneable Interface

 
Thennam Pandian
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WE know that Serializable,Set,Cloneable Interface have no

members.But by Implementing this the classes get some Features .

example a class can get Serializability by implementing the java.io.Serializable interface.

How it is Possible ? Tell me some Idea?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are "marker" interfaces which have been discussed around the ranch quite a bit the last few days. Most folks see them as an abuse of the interface feature of the language because they define no behavior. Some like Cloneable are even worse because the behavior method is there with or without the interface.

Markers are there to declare an object's interest in participating in something. If you implement serializable that means your class is willing to be serialized. The code that does the serializing can go through a graph or collection of objects and test "if (object instanceof Serializable)" to decide which objects to serialize. The serializer code won't call any methods on the object it's serializing; the presence of the interface is all it needs to know.

Java 5 introduced a new way to declare meta-data like that, so it's considered a bad practice to write new marker interfaces in that release or later.

Set, by the way, defines a ton of methods and doesn't belong in your list. I'm sure somebody will chime in with the correct list of marker interfaces in the standard libraries.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read this thread for a discussion on this:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic