I have gone through the following tutorial :
http://www.javaworld.com/community/node/2915
And after reading the above article, I feel, that it is not possible to write a Marker interface, because, how can you instruct compiler, that, what tag, it embed in the .class file for your Marker interface.
Please correct me, if I am wrong. cheers :)
Java has many built-in marker interfaces, such as Serializable, Cloneable, and Remote. Let's take the example of the Cloneable interface. If we try to clone an object that doesn't implement this interface, the JVM throws a CloneNotSupportedException.
It is an empty interface (no field or methods). Examples of marker interface are Serializable, Cloneable and Remote interface. All these interfaces are empty interfaces.
Marker Interfaces in Java have special significance because of the fact that they have no methods declared in them which means that the classes implementing these interfaces don't have to override any of the methods. A few of the marker interfaces already exist in the JDK like Serializable and Cloneable.
Instead of marker interface, Java 5 provides the annotations to achieve the same results. It allows flexible metadata capability. Therefore, by applying annotations to any class, we can perform specific action.
here tag is hexa decimal code AC ED, which is added to the .class file of that class which implements Serializable interface. So, that JVM treats this class file in a special way (may be some heavy resource allocation work), because instance of this class might be serialized. For normal classes, it adds CA FE hex.
Aha!! I understand your confusion.
CA FE
the magic number for a bytecode file; i.e. the file you get when you compile a class. The bytecode file for ANY class has this magic number, whether it is serializable or not serializable.
AC ED
is the magic number a serialized Java object file; i.e. the file you serialize an instance of some serializable class.
You are mixing up two different concepts (classes and instances) and their respective representations.
So the answer to your question is ... of course you can write your own marker interfaces! There is nothing special to the compiler about a class that implements a marker interface.
However, it would be impossible to duplicate the implementation of Java object deserialization in pure Java. Object deserialization uses a backdoor (the Unsafe.allocateInstance
method) to create objects without invoking their constructors. AFAIK, this method cannot be called from normal Java code. (And even if it can, it shouldn't be ...)
Of course you can write a marker interface. A marker interface is generally just a Interface with no methods at all (so any class could implement it).
You seem to think that marker interfaces have some magical properties that do something on their own. That's not the case. Instead some other code can react on the presence of the marker interface on some object and act differently when a class implements it. But the marker interface itself doesn't do anything .
package com.example;
interface MarkerInterface {}
Here you have one. Just copypaste it into com/example/MarkerInterface.java
, compile and use it!
Here's an usage example:
class SomeClass implements MarkerInterface {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With