Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create your own Marker interface in java?

 
Chilkosae Cozea
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

How to create own marker interface in java? How JVM will treat that interface as Marker interface and do the required functionality?

Thanks in advance.
 
Joe Ess
Bartender
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A marker interface is an interface which by its presence indicates certain functionality. It may or may not have methods. One example in the Java API is java.io.Serializable.

How JVM will treat that interface as Marker interface and do the required functionality?


The JVM treats a marker interface the same as any other interface. It is up to you, the programmer, to write code that detects and acts upon a marker interface.
Since this question is not IO related, I'm going to move it.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chilkosae Cozea wrote:How to create own marker interface in java?

Interface with no methods, which a class can implement to show that it has certain properties. So you can have

How JVM will treat that interface as Marker interface and do the required functionality?

JVM does nothing but mark the class as implementing the interface. You can then later check whether “objects instanceof Interface" and find out whether or not it is present. As in the case of java.io.Serializable, ObjectOutputStream does this job.

Please note you should avoid create new ones. Annotations introduce in Java 5 are a generic mechanism of adding metadata to a class.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic