• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Use of marker interface

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
As we know Marker interfaces are there in java, which contains no methods. As there are no methods in interface, what does it do, if we implement that interface ?
If we think in normal way what's the use of implementing an interface which doesn't contain any methods ?
Regards,
Sudhakar.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just there so someone can ask if it's there. It's a way of declaring you want to be treated in a special way at some point, but doesn't declare that you have any particular methods that can be called.
If I say "I can fly a helicopter!" somebody might ask me to do it, so I have to implement the methods.
If I say "I'm lonely" nobody will expect me to do anything special, but they might buy me lunch.
Sorry, that was goofy. But did any of it help?
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the "java.io.Serializable" interface. It has no methods to implement, but it tells the system that it's OK to serialize instances of any class that implements it. In other words SUN decided to go with an "opt in" mechanism for determining serializability (is that a word?).
Because of reflection and introspection, the JVM is perfectly capable of figuring out how to serialize any *normal* class, so unless you need special consideration, you don't have to supply any code to tell it how to serialize your object. Hence all you have to do is add the "implements java.io.Serializable" and everything is taken care of for you.
In JDK 1.5 SUN will include JSR 175 (Custom Metadata) which defines a mechanism for tagging classes with metadata that's available to the JVM. I don't see SUN doing away with java.io.Serializable, but in the future this may help do away with marker interfaces.
[ October 23, 2003: Message edited by: Wayne L Johnson ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marker Interfaces can have methods. Most of the current documentation on Marker Interfaces out there refers to Serializable and describes Markers as interfaces with no methods.

However, by conventional wisdom, Marker Interfaces can have methods associated with the interface (see Cloneable).

What, then is the difference between a common interface and a Marker Interface? If the Marker has methods, there is no difference. Here is the subtle point about Markers that have methods: they are intrinsically about "what" something is, over and above the java language enforcement of "must have methods".
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Markers are not part of Java. A class or an interface can be empty and populated later. As it turns out there are several uses for that and by convention it's called a marker interface.

Let's add some cheezy security feature. I will log the toString value of any class as long as it's marked Secure.

if( object instanceof Secure ) ...

It's a poor man's annotation but very convenient to use. It also has an Aspect Oriented Programming quality to it, but that's another story.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As has been pointed out, it's a flawed attempt at specifying metadata.
With the introduction of annotations, I don't see the argument for "convenience" (I didn't prior to 1.5 either, but it was less obvious).

@interface Serializable{} // where is the inconvenient part?
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, convenience is an argument unto itself and it's always a valid one. What it's not, always, is a decisive argument. Annotations are almost as convenient so my preference would also be to annotate.

It's not quite as convenient since it's a little more elaborate to define than an interface.

>> @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Secure { }

Then you have to fetch it rather than use instanceof. No biggie, but not as convenient:

>> if( klass.isAnnotationPresent(com.acme.Secure.class) ) ...
[ August 23, 2005: Message edited by: Rick O'Shay ]
 
author and iconoclast
Posts: 24206
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chuck Herrick posted to this two-year-old thread to start this current conversation, and I just wanted to point out that his premise is wrong: Cloneable does not have a "clone()" method -- that method is in java.lang.Object. The design of cloning in Java is very weird and it's a bad idea to hold it up as an example of anything.

But in any case, Chuck: a marker interface, by definition, has no methods. That's what makes it a marker interface. If it has methods, then it's not a marker interface, just a plain old interface interface.
 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic