wood burning stoves 2.0*
The moose likes Java in General and the fly likes marker interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "marker interface" Watch "marker interface" New topic
Author

marker interface

Rambabu Gonela
Greenhorn

Joined: Jul 31, 2006
Posts: 24
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Rambabu Gonela
Greenhorn

Joined: Jul 31, 2006
Posts: 24
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
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Rambabu Gonela
Greenhorn

Joined: Jul 31, 2006
Posts: 24
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

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: marker interface
 
Similar Threads
marker interface
Marker Interface doubt
serializable interface
regarding marker interfaces
How to write our own Marker Interface?