This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes How does a class gets that functionality from marker interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How does a class gets that functionality from marker interface?" Watch "How does a class gets that functionality from marker interface?" New topic

How does a class gets that functionality from marker interface?

Sanjaykar Gurram

Joined: May 05, 2008
Posts: 22
How does a class which implements a marker interface( like RandonAccess which is implemented by ArrayList class) gets that ability even though no method declarations are made in the Marker Interface?

Does the JVM provide the extended functionality by implementing the Marker Interface?

ArrayList which implements RandomAccess(Marker Interface) gets the ability of fast random access. How it is possible? A marker interface has no methods right?
Campbell Ritchie

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

In some cases (eg Cloneable, Serializable) the JVM looks for that interface before implementing that functionality. The functionality is implemented in the writeObject() or clone() methods.

In the case of RandomAccess there is no functionality to implement. ArrayList implements RandomAccess because it allows fast access to any element. More details in the API for RandomAccess.
Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1280

Hi sanjaykar,

you're right, a marker interface doesn't provide methods to implement. It really just tells you something about a class, e.g. if a class provides random access or is serializable. There's nothing mysterious about marker interfaces. With current Java versions you could better use annotations for this purpose.

Here you can get some more information!

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41089
Please use the "Search" link at the top of the page to find the many, many extensive previous discussions about this topic.

Ping & DNS - my free Android networking tools app
Rob Spoor

Joined: Oct 27, 2005
Posts: 19651

RandomAccess could be used as follows:

This will allow you to save the creating of the Iterator if the List also implements RandomAccess. I've never done this though; IMHO, an Iterator has little extra overhead in both memory and speed and therefore not worth the trouble. In fact, the ArrayList's Iterator implementation (actually implemented by AbstractList) uses get(int) in the background as well. It's LinkedList that has a special implementation that uses the special structure of the list.

How To Ask Questions How To Answer Questions
It is sorta covered in the JavaRanch Style Guide.
subject: How does a class gets that functionality from marker interface?
Similar Threads
How the Marker Interfaces provides the functionality to its object implementation?
How does a marker interface work?
How to create your own Marker interface in java?
How to write our own Marker Interface?
Marker Interface