• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt about marker Interface  RSS feed

 
Harivenkatesh Polnati
Greenhorn
Posts: 25
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt about marker interface
by the meaning of marker interface it does not containing any methods.so, what is the use of marker interface please explain with example
 
Tim Moores
Saloon Keeper
Posts: 4032
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has been asked many, many times; please avail yourself of the "Search" link at the top of the page. There's really nothing to add to those previous answers at this point.
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marker interface are used to inform the compiler to add special behavior to the class implementing it.
E.g java.io.Serializable, tells the compiler that the objects of the class implementing Serializable interface can be serialized.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We've discussed this question many times before. If you do a search, you'll find many older topics that answer your question. For example:

marker interface
Marker interface
what is the meaning of marker interface?
etc.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumit Patil wrote:Marker interface are used to inform the compiler to add special behavior to the class implementing it.
E.g java.io.Serializable, tells the compiler that the objects of the class implementing Serializable interface can be serialized.

The compiler doesn't do anything special when it encounters a class that implements a marker interface*.
The built in marker interfaces (such as Serializable or Cloneable) are used by the JVM at runtime to decide if certain operations can be performed on an object.
But you can also create your own marker interfaces** which other parts of your code can then use to make decisions about how your code works.



* Unless you have declared a reference variable of the type of the marker interface, in which case the compiler may check if an object being assigned to this reference variable actually implements the interface. Although as the interface has no methods or fields I can't see any obvious reason for doing this.

** Although these days it is often recommended to use annotations rather than marker interfaces.
 
BalaMurali dhar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marker interfaces are those which do not declare any required methods,but signify their compatibility with certain operations. For Example, The java. io. Serializable interface and clonable are typical marker interfaces. They do not contain any methods, but classes must implement this interface in order to be serialize and de-serialized
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!