• Post Reply 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

what is marker interface?

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marker interface means no methods in it. Then why such interfces are provided in java API?
 
Ranch Hand
Posts: 454
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


The marker interfaces are used to provide certain functionality to classes you code. Take for instance the Cloneable interface. This interface is implemented by the JVM itself and allows shallow copies of objects to be created without the developer having to write code for this purpose. User written marker interfaces can also be used for inheritance purposes.

Cheers,
Raj.
[ May 14, 2008: Message edited by: Raj Kamal ]
 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever we implementes Interfaces then it is compulsory to the programmer that he/she should override all the methods in concreate classes which are declared there in the Interface.

If marker interface do not have any methods then no question of overiding. What difference it will make if we are not implementing marker interface?
 
Marshal
Posts: 76107
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason for marker interfaces (also called tagging interfaces) is that a class using them can test

. . . if (myObject instanceof FooMarkerInterface) . . .

It is probably better to use annotations since Java5.
We have FAQ, and you will find more about marker interfaces there.
If you need more information, please post a reply.
 
Sheriff
Posts: 22665
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marker interfaces can be used with the instanceof operator; for instance, if a List implementation also implements RandomAccess it is more efficient to iterate numerically, using the get method. If it doesn't, iterator() should be used.

Cloneable and Serializable have similar uses, only internally in the JVM.
 
Author
Posts: 3461
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The interfaces with no defined methods act like markers. They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently. Example java.io.Serializable, java.lang.Cloneable, java.util.EventListener etc. Marker interfaces are also known as �tag� interfaces since they tag all the derived classes into a category based on their purpose.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here i have doubt. Can we create our own marker interface .

Yes i think we can create . Could any one tell where we can create marker
interface ?


Praveen
SCJP | SCWCD(Progress)
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pillai, a small correction here. Marker interfaces are not compiler special. We can create our own marker interfaces and use them as seen fit. I guess the explanations given till now do a great job of explaining them.

Originally posted by arulk pillai:
The interfaces with no defined methods act like markers. They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently. Example java.io.Serializable, java.lang.Cloneable, java.util.EventListener etc. Marker interfaces are also known as �tag� interfaces since they tag all the derived classes into a category based on their purpose.

 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pdommara Kumar:
Could any one tell where we can create marker interface ?


How would you create an interface with a single method in it?

If you know that, then how would you create an interface with no methods in it?
[ May 14, 2008: Message edited by: Ulf Dittmer ]
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marker interfaces are not compiler special.


Indeed, the compiler has nothing to do with it. It's the JVM and the class libraries (and user-supplied classes) that implement the desired behavior.
 
Campbell Ritchie
Marshal
Posts: 76107
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Ulf has hinted, it is easy to create one's own marker interface.

But it is probably not a good idea to try . . .
 
reply
    Bookmark Topic Watch Topic
  • New Topic