This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv 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 REST with Spring (video course) this week in the Spring 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: 45348
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: 1294

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: 42959
Please use the "Search" link at the top of the page to find the many, many extensive previous discussions about this topic.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

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
I agree. Here's the link:
subject: How does a class gets that functionality from marker interface?
It's not a secret anymore!