Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marker Interfaces in Java

 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tanu Gulati
Ranch Hand
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic