This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Purpose of Marker Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Purpose of Marker Interface

Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45



Hi All,
Iam still not cleared with the purpose of Marker Interface inspite of reading many articles.I think one way communiction on this topic will not help me in understanding and so this thread.

Cloneable is a Marker Interface.

When we call clone() on an object - this will throw CloneNotSuppException if the class does not implement Cloneable interface.

So, I need to implement Cloneable - Whats the advantage/benefit/point here.Why not the developer just can call clone() irrespective of not implementing Cloneable interface???

Can anybody explain me here?

Thanks.

Regards
Senthil Kumar Sekar


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Cloneable and clone() is a bit of a weird design (dating back to the earliest version of Java), so it's probably not a good example of how a marker interface should be used*. But it does demonstrate how they can be used.

A better example is RandomAccess. This is a marker interface used to indicate whether a List can support efficient random access. ArrayList implements RandomAccess, but LinkedList does not. The idea is that an algorithm using a list can check for this (using instanceof), and can use the best approach depending on whether random access is efficient or not.

(* Some would say that nowadays a marker interface should never be used, because annotations are a better solution. They weren't available in the earliest versions of Java).
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7024
    
  16

Senthil Kumar Sekar wrote:Iam still not cleared with the purpose of Marker Interface inspite of reading many articles.

To be honest, neither are we; although we all have opinions.

You may be interested in this thread, where I was soundly outvoted...but I'm sticking to my guns (I'm nothing if not stubborn).

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Purpose of Marker Interface
 
Similar Threads
Cloning in java
Reg : marker interface
Cloning in java
What is Marker interfaces?
marker interface