aspose file tools*
The moose likes Java in General and the fly likes Marker Interfaces in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Marker Interfaces in Java" Watch "Marker Interfaces in Java" New topic
Author

Marker Interfaces in Java

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

we implement the methods defined in Interface and many classes can implement as per their requirement then how marker Interfaces come in to picture.
i did some gooogling and found this

Since Cloneable is a marker interface and implementing this interface will qualify this class to be of creating a clone.

Same is the issue with Serialization, If we are not defining any thing in the interface how it will make the implementing classes following the serializtion process.

My question is how is this possible is there some thing behind the scence of this Interface happening?
thanks,

[ October 23, 2008: Message edited by: Tanu Gulati ]
[ October 23, 2008: Message edited by: Tanu Gulati ]

thanks
Tanu Gulati
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42282
    
  64
Yes, something is happening behind the scenes. Since this question has been asked many, many times, please use the "Search" link at the top of the page to find the previous extensive discussions on this topic. Make sure to search through all forums, not just this one.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Marker interfaces allow you to do an instance of check:

That's probably just what is happening with Serializable, but then inside the JVM itself.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

I think the phrase "behind the scenes" was a little ambiguous i am sorry for it.

what i mean to say is suppose i create a Marker Interface say "GuidGenerator" whose job is to generate the Guid for the implementing class objects.

My understanding is that i have to write some helper classes for this. which will gets executed as soon as some class implement "GuidGenerator" marker Interface.

but i dont know if it is possible if yes how should i structure my code and link all the threads along with it..
thanks,
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42282
    
  64
No, that's not an example of marker interfaces. In that case the interface would have one method called "getGUID" or something like that, and all objects implementing the interface would have that method.

But as I said, marker interfaces have been discussed endlessly in these forums. Use the Search feature to find those discussions.
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

I tried finding but i am sorry to say that i am not able to get the specific answer to my question and one more interesting fact i found that every where people asked about how to create the our own marker Interface and use it but i found every where person who answered did not understand the question properly.

the question is NOT what is marker interface, Question is HOW to create and use our own marker Interface in our project.

May be previous example was not good enough here is one more

Suppose i wanted to have one marker interface say "SaveDateTimeInterface" and its functionality is to save the date and time of the object being saved in to database.

Classes who dont implement this Marker interface, their objects will not be having the date and time values saved in the database.

Hope this example is little more clear than previous.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Originally posted by Tanu Gulati:
Suppose i wanted to have one marker interface say "SaveDateTimeInterface" and its functionality is to save the date and time of the object being saved in to database.
[/QB]


That is where you are getting things confused.
Marker interfaces are NOT supposed to have any methods declared inside them. What you have said is just an example of any other normal interface.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
Tanu,

You are clearly confused. When something is 'serializable', the check actually happens when the object is getting serialized. Somewhere in the ObjectOutputStream there is a check like


So if you want to use your custom marker interface may be you should impose this restriction(of storing date and time) at the time of entering the values into the database. Is this making sense?


Cheers!
RSR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marker Interfaces in Java