File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is advantage of marker interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is advantage of marker interface?" Watch "What is advantage of marker interface?" New topic
Author

What is advantage of marker interface?

Rajesh Varma
Greenhorn

Joined: Aug 07, 2006
Posts: 7
If there are no methods in a marker interface, then what's the use?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

For more :
http://faq.javaranch.com/view?JavaIntermediateFaq

To make money with marketing

Anybody making money on Clonable ?


[My Blog]
All roads lead to JavaRanch
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Since Java 5, you should use annotations instead of marker interfaces.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
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.


42
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Apr 21, 2006
Posts: 1847
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
Dana Bothner-By
Ranch Hand

Joined: Jul 28, 2006
Posts: 37
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

Joined: Apr 21, 2006
Posts: 1847
I said it was a somewhat contrived example
Nishant Modak
Greenhorn

Joined: Sep 29, 2006
Posts: 27
You people have confused me , by going through all this.!

A summary for the explaination anybody ?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is advantage of marker interface?