aspose file tools*
The moose likes Java in General and the fly likes How to write our own Marker Interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to write our own Marker Interface?" Watch "How to write our own Marker Interface?" New topic
Author

How to write our own Marker Interface?

Sreeni Gundla
Greenhorn

Joined: Dec 21, 2009
Posts: 7
How to write our own Marker Interface..? how to write a Marker Interface similar to serilizable Interface.

We can write our Marker as interface ExampleMarker { } similar to Serializable interface.
But how to define the functionality as there some meaning in implementing Serializable interface.

Please suggest me.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
Welcome to JavaRanch.

The JRE wouldn't know anything about your marker interface, so any special treatment would have to happen in the code you write. Probably in sections such as "if (someObject instanceof ExampleMarker) { ... }".

You shouldn't create marker interfaces, though. That's what annotations are used for these days.


Ping & DNS - my free Android networking tools app
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
marker interfaces usually don't come with functionality. That's why it's called a marker interface.

It simply tags a class as something.

A good example is the SingleThreadModel in the Java Servlet world.

Usually servlets run multithreaded. If you need your servlet not to run concurrently, you implement the SingleThreadModel marker interface This does not add any functionality, since this is not needed. It just tells the underlying framework 'Oh, watch out, no multithreading here'

It could look like this.


JDBCSupport - An easy to use, light-weight JDBC framework -
Sreeni Gundla
Greenhorn

Joined: Dec 21, 2009
Posts: 7
Ulf Dittmer wrote:Welcome to JavaRanch.

The JRE wouldn't know anything about your marker interface, so any special treatment would have to happen in the code you write. Probably in sections such as "if (someObject instanceof ExampleMarker) { ... }".

You shouldn't create marker interfaces, though. That's what annotations are used for these days.


Hi Thanks for your reply.
i am really worried about this marker interface ... as i have not written my own maker interface ... in my exp. as of now.
I do really face this question while attending interviews... they told that we can write our own marker interface and inform JVM about it.....
still finding for a solution on this.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
Not sure what, exactly, "inform the JVM about it" means, but the JVM will not know anything about marker interfaces you create, ever. All the implementation happens in code of yours, not in the existing class libraries.

The best answer you can give is to point out that marker interfaces were a bad idea to begin with, and that now annotations should be used instead.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Ulf Dittmer wrote:Not sure what, exactly, "inform the JVM about it" means, but the JVM will not know anything about marker interfaces you create, ever. All the implementation happens in code of yours, not in the existing class libraries.

The best answer you can give is to point out that marker interfaces were a bad idea to begin with, and that now annotations should be used instead.


This is was an interesting question really .
What I understand from you answer Ulf is that there is no real way in which we could write a true marker interface in the 'Cloneable' sense where special functionality can be attached to a class just be virtue of it implementing a interface.

The closest we can get is create an empty interface & then use code to check if an object implements that interface it gets some functionality. Yep sounds bad - time to read up on annotations!

Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
Sam Mercs wrote:there is no real way in which we could write a true marker interface in the 'Cloneable' sense where special functionality can be attached to a class just be virtue of it implementing a interface.

That's not quite correct. Functionality *can* be attached to classes/objects by virtue of them implementing a marker interface, but one needs to write the code to do so. You can't expect the JVM to know what to do with a class that implements "SamsMarkerInterface" :-)

time to read up on annotations!

Annotations are no different in this respect. The JVM wouldn't know what to do with "SamsAnnotation" either - you'd have to write the code that makes use of that, too.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Ulf Dittmer wrote:
Sam Mercs wrote:there is no real way in which we could write a true marker interface in the 'Cloneable' sense where special functionality can be attached to a class just be virtue of it implementing a interface.

That's not quite correct. Functionality *can* be attached to classes/objects by virtue of them implementing a marker interface, but one needs to write the code to do so. You can't expect the JVM to know what to do with a class that implements "SamsMarkerInterface" :-)

time to read up on annotations!

Annotations are no different in this respect. The JVM wouldn't know what to do with "SamsAnnotation" either - you'd have to write the code that makes use of that, too.


Yep agree on both points - the annotation articles was pretty explanatory on both the points you stated.
(Probably should have read before commenting )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to write our own Marker Interface?