• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

marker interface

 
Rambabu Gonela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
we all know marker interface does't have any methods...but it has some functionalities.. how will it get fuctionality..(dont tell jvm provides that functionalities)
and
can we create marker interface(that has some functionality)
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A marker interface doesn't have any functionality. Marker interfaces are used by other code to test for permission to do something. For example, the Serializable interface is used by the implementation of serialization like this:



That's all!

Marker interfaces are an old-fashioned concept and you probably shouldn't define new ones; annotations, introduced in JDK 1.5, are widely held to be a superior replacement.
 
Rambabu Gonela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we create marker interface if so where do we specify its functionality.

i.e., if i create an marker interface how would the client code know what functionality does our marker interface would providem.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ernest said, the interface doesn't provide any functionality. It's the client code that can do something like the code shown above to implement particular functionality for classes that implement the interface.
 
Rambabu Gonela
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any object that implements the java.io.Serializable interface can be made as serialazable., that means JVM performs serialization internally...Similarly if i define a marker interface, how would JVM know what task to be performed on behalf of my marker interface. How should i intimate the JVM about the task that should be performed by it, if any object implements my marker interface?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rambabu Gonela:
Can we create marker interface if so where do we specify its functionality.

i.e., if i create an marker interface how would the client code know what functionality does our marker interface would providem.

Marker interfaces specify metadata about a class (i.e., to indicate that a class has a certain capability, for example that it is serializable or cloneable).

Ofcourse you can create your own marker interfaces, but since Java 5.0 there is a new and better mechanism for metadata: annotations. So if you feel you need to write your own marker interface and you're using Java 5.0 or newer, then learn about annotations.

Creating marker interfaces should be considered deprecated since Java 5.0.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you create your own marker interface or annotation, the JVM ofcourse doesn't know what it means. You should write your own code somewhere that handles this. You can use instanceof to see if an object is of a type that implements a specific marker interface. For example:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic