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

What is advantage of marker interface?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there are no methods in a marker interface, then what's the use?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajesh Varma:
If there are no methods in a marker interface, then what's the use?



I can think of a few reasons:
- to confuse people
- to write books about
- to make money with marketing
- to examine scholars on

If you are referring to their use in software then the answer is that there is none. Do not use an interface for contract metadata.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For more :
http://faq.javaranch.com/view?JavaIntermediateFaq

To make money with marketing


Anybody making money on Clonable ?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marker interfaces are sometimes used to indicate that an object is a particular kind of thing, or suitable for a particular application. As they do not have any methods, they can only really be used in things like "instanceof" expressions.

They are a messy and unappealing idea, most of the time, as Tony M has indicated in his inimitable style. Even if one doesn't go along with his total ban on the things, they're something to be used at most very sparingly. In fact, anything with "instanceof" in it is best avoided where possible.

Java itself has some marker interfaces, some of which you are pretty much forced to use, even if you hate them. Serializable is one that you'd find it seriously hard to avoid. Cloneable is another built-in marker interface, but in my view the whole cloning feature of the language is so badly designed as to make it worth ignoring 99% of the time.
[ August 16, 2006: Message edited by: Peter Chase ]
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, but I think that Serializable is worth using. It is useful and does have a narrow contract. Like everything else, when it makes sense it is powerful, if you use it on anything and everything it can cause trouble.

Imagine if BufferedReader or SeverSocket were serializable. The fact that ArrayList is gives a solid and simple solution if you ever need to save its state.

It can aid in encapsulation, if you have objects of a serializable class and you need to save its state for later, you can save them to file with little fuss, and no need for accessors which some frown on. Otherwise you will have to go through convoluted steps to get all the pertitant member values and then write it to a file, then do the reverse when you need to recreate the object. Messy and error prone.

The others I wouldn't waste time on.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since Java 5, you should use annotations instead of marker interfaces.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that tidbit Ilja. I have been slowly poking into the new Java 5 stuff, I will look into it if/when I get a good grasp of generics.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Since Java 5, you should use annotations instead of marker interfaces.



That depends on the philosophy you follow

If a marker interface just tells you something about the class, then yes.
If the marker interface tells you the class is something special, then a marker interface is a valid OO construct even if it doesn't add functionality.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeroen T Wenting:

That depends on the philosophy you follow

If a marker interface just tells you something about the class, then yes.
If the marker interface tells you the class is something special, then a marker interface is a valid OO construct even if it doesn't add functionality.



I'm not sure I understand the difference. Can you give an example for the latter? Thanks!
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serializable doesn't do much, except prevent an exception

Another marker interface might tell you that a class has a special meaning though, rather than guaranteeing it's simply well behaved.
Say you have a marker interface Trainee. An employee who is a trainee has no need for special methods, but other parts of the system may treat them differently (like no profit share at the end of the FY, or no invitation to the company Christmas party).
To me at least that's a fundamental difference, that traineeship is something that's not just tagged on like an annotation, it's a fundamental part of what that particular person is.

Maybe not the strongest example, but you get the point.

Of course I don't like annotations in general, so will try to avoid them most of the time
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the advantage of the marker interface Trainee versus having the method

public boolean isTrainee()

in class Employee? Why would you want to build Trainee-ness into the static type structure of your classes?
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I said it was a somewhat contrived example
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You people have confused me , by going through all this.!

A summary for the explaination anybody ?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Summary:

There are some marker interfaces in the library. Implement them when you want to indicate something to other library classes that look for them.

Opinion: They were not a very good idea in the first place, maybe a practical workaround, maybe downright evil. Don't create any more of them in your own code.

Opinion: Annotations are a better way to accomplish the same thing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic