This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How the Marker Interfaces provides the functionality to its object implementation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How the Marker Interfaces provides the functionality to its object implementation?" Watch "How the Marker Interfaces provides the functionality to its object implementation?" New topic
Author

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

Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2885
    
  13

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)


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mihir Patel
Greenhorn

Joined: Apr 26, 2011
Posts: 18

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

Joined: Oct 25, 2008
Posts: 2700

Also see our FAQ entry in our Java-FAQ.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

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
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
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.

Ping & DNS - updated with new look and Ping home screen widget
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How the Marker Interfaces provides the functionality to its object implementation?
 
Similar Threads
Regarding Marker Interface?
Can I make an Interface as marker Interface
How does marker interfaces work?
marker interface
Marker Interface