• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 18
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see our FAQ entry in our Java-FAQ.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic