This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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)
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:
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.
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.
Joined: Jul 31, 2006
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?
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.
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: