• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

marker interface

 
Greenhorn
Posts: 11
Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do we have any means to create our own marker interface??
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A marker interface is an interface which does not contain any methods or fields. So yes, it is easy to create such an interface, but it is usually seen as a bad practice. You should use annotations instead of marker interfaces in Java 5 or later.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

aryan sahu wrote:do we have any means to create our own marker interface??



Since a marker interface is just an interface that doesn't define any methods, simply define an interface and don't give it any methods.

If you're asking if there's a way to give it meaning for the language or the core API, then, no, we can't do that. It's only meaningful to code that knows about it and decides to interpret it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vajsar wrote:A marker interface is an interface which does not contain any methods or fields. So yes, it is easy to create such an interface, but it is usually seen as a bad practice. You should use annotations instead of marker interfaces in Java 5 or later.



That depends on what you're trying to do. If you want to define a type for a certain amount of compile-time type-safety for some purpose, then a marker interface can provide that, but an annotation can't. (Not that I can think of a particular case where one might need that, but that doesn't mean there isn't one.)
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:That depends on what you're trying to do. If you want to define a type for a certain amount of compile-time type-safety for some purpose, then a marker interface can provide that, but an annotation can't. (Not that I can think of a particular case where one might need that, but that doesn't mean there isn't one.)


The only use of marker interfaces I've ever seen was testing its presence on an object using the instanceof operator. I'm not very skilled in annotations, but I assume it can be always done with them.

How to provide type safety using marker interface? Since it by definition does not contain any method, even if you declare a variable or parameter of that type, you won't be able to call any methods* on it without casting it first, which breaks the type safety. Let's say you create a method which requires a list that supports random access. You could ensure this by declaring the parameter as RandomAccess, but it certainly smells bad. If RandomAccess extended List, it would be something different, but in this case it would not be a marker interface (as it would have inherited some methods). Though the idea is entertaining.

* You can call Object's methods on such variable, but you can do so on any object variable in Java, so this is moot.
 
There's a city wid manhunt for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic