Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Purpose of Marker Interface

 
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Report post to moderator


Hi All,
Iam still not cleared with the purpose of Marker Interface inspite of reading many articles.I think one way communiction on this topic will not help me in understanding and so this thread.

Cloneable is a Marker Interface.

When we call clone() on an object - this will throw CloneNotSuppException if the class does not implement Cloneable interface.

So, I need to implement Cloneable - Whats the advantage/benefit/point here.Why not the developer just can call clone() irrespective of not implementing Cloneable interface???

Can anybody explain me here?

Thanks.

Regards
Senthil Kumar Sekar


 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Report post to moderator
Cloneable and clone() is a bit of a weird design (dating back to the earliest version of Java), so it's probably not a good example of how a marker interface should be used*. But it does demonstrate how they can be used.

A better example is RandomAccess. This is a marker interface used to indicate whether a List can support efficient random access. ArrayList implements RandomAccess, but LinkedList does not. The idea is that an algorithm using a list can check for this (using instanceof), and can use the best approach depending on whether random access is efficient or not.

(* Some would say that nowadays a marker interface should never be used, because annotations are a better solution. They weren't available in the earliest versions of Java).
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator

Senthil Kumar Sekar wrote:Iam still not cleared with the purpose of Marker Interface inspite of reading many articles.


To be honest, neither are we; although we all have opinions.

You may be interested in this thread, where I was soundly outvoted...but I'm sticking to my guns (I'm nothing if not stubborn).

Winston
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic