Marker Interfaces

Can someone explain the contract with marker interfaces in java?

For Ex: if Clonable is a marker interface without fields / methods, then where is clone() defined?

Why should we implement Clonable i / f when clone() ?

Well, my question is: if clone() is a method of the java.lang.Object class, why use Clonable i / f to override clone() .

Can anyone describe this java convention?

Thanks at Advance

+6
source share
6 answers

clone() defined in the java.lang.Object class, from which all classes are distributed, but it is protected . This is actually a concrete implementation of a method that executes a field according to an objectโ€™s quota, but only if you implemented the Cloneable interface to indicate that it is allowed.

In practice, many people override the clone() method so that they can make it public and allow cloning from outside the class.

This whole picture is rather unusual, and not what you usually replicate, I cannot come up with many other examples in the JVM, where there is a paired interface and a marker method. Starting with Java 5 , it is better to use annotations for markers. for example @XmlRootElement , used to designate a type as serializable Jax-B (post Java 5) and the Serializable interface (pre Java 5) used to designate a class, is binary serializable.

+7
source

What is a marker interface?

An interface that does not contain any method for implementation is called a marker or tag interfaces.

Why are marker interfaces?

The basic idea of โ€‹โ€‹having marker interfaces is that the class implements an interface whose behavior is implicit. A class does not have to implement anything to fit the contract defined by the interface. On the contrary, this means that the JVM indicates the expected functionality to execute implicitly.

Java example

  • Serializable - allowed to serialize objects of this type. Remote - This type is suitable for remote calls to the Clonnable method. make a copy of instances of field instances for the field

Can we create marker user interfaces?

Yes it is possible.

+2
source

Clonable does not contain the clone() method, which is protected in java.lang.Object .

Additional information is available here.

Quote from Josh Bloch Effective Java:

"The Cloneable interface was intended as a mixin interface for advertising objects that allow cloning. Unfortunately, this is not suitable for this purpose ... This is a very atypical use of interfaces, not emulation ... In order to implement the interface for any effect on class, he and all his superclasses must obey a rather complex, impossible, and largely undocumented protocol. "

+1
source

The java.lang.object class is the super / parent class of all java classes, if you want to create an object in java, then it must implement the java.lang.object class. If you do not import the class superclass into your code, then the compiler will implicitly import it into your code. SO automatically all its properties and behavior are available to you - an object (program), including the clone () method, if you call the clone () method in your program, which means that the clone () method is called from the superclass (class Object), and not from a child class.

Marker Interfaces: Its true Marker interfaces are an empty interface that does not contain properties and behavior. Now the question can be raised.

Q. Who will implement the predefined marker interface, if used in our program?

The answer . The JVM will assume this responsibility because the functionality of the marker interface is defined inside the JVM, so it implements and adds some additional functions for you - this is a program.

Therefore, the programmer does not need to implement the Clonable Marker interface, the JVM will assume this responsibility.

+1
source

Token interface is a common method for marking classes. They do not add behavior to classes (in general). The Clonable interface is such a tag: each class marked with Clonable is capable of cloning itself (this is the rule).

The same thing with Serializable , although another hidden magic is hidden behind this marker interface (the serializer looks for some methods and fields that can be implemented by the marked class)

Bonus information: forget about Clonable , it broke. If you want to create clones in real life, find a copy constructor template.

0
source

Token interfaces do not have any body as such. Just ask the java interpreter to behave in a certain predefined way for class objects that extend them.

-1
source

All Articles