Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does a class gets that functionality from marker interface?

 
Sanjaykar Gurram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the "Search" link at the top of the page to find the many, many extensive previous discussions about this topic.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic