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

How the Marker Interfaces provides the functionality to its object implementation?

 
Mihir Patel
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone please let me know how the marker interface provides the functionality to its object implementation?

What i know is that Marker interfaces doesn't define any members. its an empty interface without any methods then how it provides the functionality to the objects which implements this marker interface.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4588
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marker interfaces are used to 'mark' your objects. A good example is java.io.Serializable interface. If you make your own marker interface, you can check whether your objects are 'marked' by using an instanceOf check, as in if(myObject instanceOf Serializable)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A marker interface does not provide any functionality to a class by itself. You can use instanceof to check if an object is of a class that implements the marker interface, and then do whatever you want to do:

In the standard Java library, marker interfaces are used in a few places. The most well-known are java.io.Serializable and java.lang.Cloneable. You can use these on your own classes to indicate that it should be possible to serialize or clone objects of your class. Other code in the standard Java library then uses instanceof to check for these marker interfaces when you try to serialize or clone an object of your class.
 
Mihir Patel
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply..

but i want to know that if your class is serialized using serialized interface then how the state of the objects of the serialized class saved, means where the methods writeObjedct() and and readObject() are defined and implemented to save the state of the objects?

thanks...
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see our FAQ entry in our Java-FAQ.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The writeObject and readObject are optional methods that you can implement in your class. When they are there, they will be called by the JVM when your object is serialized or deserialized. If you don't implement these methods, then the JVM will serialize and deserialize the object using a default format.

These methods are not defined in any interface anywhere. They are special methods that the JVM recognizes. This doesn't really have anything to do with how marker interfaces work; it's just the way how serialization is built-in into Java.

The API documentation of java.io.Serializable explains it in more detail.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the FAQ entry Wouter linked to states that you should not create any new marker interfaces; it's what annotations are used for nowadays.

Jesper wrote:The most well-known are java.io.Serializable and java.lang.Cloneable. You can use these on your own classes to indicate that it should be possible to serialize or clone objects of your class.

I agree, yet these two, especially Cloneable, are also a major reason why so many people get confused over the whole issue of marker interfaces. Cloneable is about allowing access to the clone method, which is a really weird design: the interface should have had the clone method in it (thus not being an actual marker interface).

Jesper wrote:(The readObject and writeObject´╗┐) methods are not defined in any interface anywhere. They are special methods that the JVM recognizes. This doesn't really have anything to do with how marker interfaces work; it's just the way how serialization is built-in into Java.

Yeah, and it's a bad design IMO. Serialization customization should arguably have been left to the Externalizable interface.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic