aspose file tools*
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
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
Author

How does a class gets that functionality from marker interface?

Sanjaykar Gurram
Greenhorn

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
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
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
Bartender

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!

Marco
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41809
    
  62
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
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does a class gets that functionality from marker interface?