How does a class gets that functionality from marker interface?
Sanjaykar Gurram
Greenhorn
Joined: May 05, 2008
Posts: 22
posted
0
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: 32644
4
posted
0
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.
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.
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.