• Post Reply Bookmark Topic Watch Topic
  • New Topic

why we need to implement serializable to save our objects ?  RSS feed

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we have to implement all the method of a interface
but with serializable interface we dont have to implement any methods why so ? is that interface does not contain any abstract method if yes why we need to implement it to save our objects into a file ?

[edited for language]
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serializable is a "marker interface". If you implement it, then Java understands that objects of that class are meant to be serialized. Serialization works by special handling by the JVM. It's not really an elegant solution, but that's what the Java and JVM designers chose long ago when they invented Java. I think that if they'd have to invent it again, they would do it without this special "magic" interface; they'd probably invent an annotation instead of a marker interface. But annotations didn't exist in the time of Java version 1.0.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, maybe a serializable keyword to match the transient keyword.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that if annotations had been invented earlier, also transient would have been an annotation. It's typically a "metadata" keyword, exactly what annotations are for.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: you would have @Serializable and @Transient.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!