It's not a secret anymore!
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

rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
What really is the use of marker interfaces if they do not have any method definitions?

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Before Java 5, they were a substitute for class level annotations.

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
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Welcome to the Ranch.

You are probably right about "what is the use?" There is more information about marker interfaces in these FAQ, with another useful link. You will have to scroll down the FAQ a bit to find marker interfaces (also called tagging interfaces).
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
I still dont get it. To quote java faqs

Marker interfaces are a misuse of interfaces, and should be avoided.

Why? And How have annotations made things better?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

Originally posted by rakesh sugirtharaj:
I still dont get it. To quote java faqs

Why? And How have annotations made things better?

Because interfaces should describe functionality - the methods an object will have implemented and how they behave. Additionally, they can be used (abused as well?) for storing constants, like javax.swing.SwingConstants.

Annotations on the other hand give information about a class, method, field, etc (including annotations themselves). Is it deprecated, does the method override another method, etc. Marker interfaces like Serializable and Cloneable do the exact same thing, and could (should) be replaced by annotations like @Serializable and @Cloneable, since they give information about the class.
They remain because of backwards compatibility.

Now what Sun maybe should have done is start using annotations, make the marker interfaces deprecated and have the JVM check for presence of either (as to not break old code). Since they haven't, these marker interfaces will not be removed since everybody will continue using them.

How To Ask Questions How To Answer Questions
Malayathi Partha Saradhi

Joined: Nov 30, 2007
Posts: 10

Marker interfaces which doesn't have any methods i.e. indicates a rule or protocal.If implements those marker interfaces ,compiler understand and supply by seeing neccessary environment to that classes whichever implements marker interfaces.

Thank you.
I agree. Here's the link:
subject: marker interfaces
It's not a secret anymore!