• Post Reply Bookmark Topic Watch Topic
  • New Topic

Marker Interface methods ?  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marker Interface means they do not have any methods. But Clonable interface has clone() method, Serializable Interface has readObject() and witeObject() methods. How ?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither Serializable nor Cloneable has any methods (check their javadocs). That there are methods related to their function that are not part of the actual interfaces indicates a screwed-up design.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:That there are methods related to their function that are not part of the actual interfaces indicates a screwed-up design.


Quoted for truth.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I can see one reason why the methods are not in the interface - that requires them to be public. For Cloneable / clone() I still don't see any problems with that. readObject / writeObject however are intended solely for the serialization mechanism. That's why they're private. If they are part of the interface then they must become public, and any code can call them. For writeObject that probably isn't a real problem, but readObject may leave the object in an inconsistent state, especially if the passed ObjectInputStream's current data does not represent the object.

On a related note: why doesn't ObjectOutputStream.writeObject take Serializable as its parameter instead of Object? Any non-serializable object will lead to an exception, so why not limit it through the compiler?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!