File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes marker interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "marker interfaces" Watch "marker interfaces" New topic

marker interfaces

Christie George

Joined: Aug 16, 2004
Posts: 19

Why do designers go in for marker interfaces while designing? Whats the advantage
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Marker interfaces (or tagging interfaces, as they are normally known) are basically used to "signal" something to the users of your classes. You would better understand with an example. Consider the method :

Now, this method may either recieve a LinkedList or it may recieve an ArrayList, or even some new List type that you are not even aware of! You never know with what type of a List this method will be called. However, you want to make sure that your sorter() method is as optimized as possible. How would you do this?

You might be aware that there is a tagging interface in the collections library called RandomAccess. The ArrayList implements this interface. Why? Its just to signal that random access for that particular container is going to be efficient. Random access in an ArrayList is efficient and the same on a LinkedList is definitely not. So, you can use this tag to your advantage in your sorter() method and modify the method as follows:

Does this make the concept clear?

Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

There's another thread here in javaranch
that you may readup.

I agree. Here's the link:
subject: marker interfaces
jQuery in Action, 3rd edition