Marker Interfaces

喜夏-厌秋 提交于 2019-12-01 18:11:41
EdC

clone() is defined in the java.lang.Object class which all classes extend from, however it is protected. This is actually a concrete method implementation that does a field by field clone of the object, but only if you've implemented the Cloneable interface to indicate this 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 pattern is quite unusual and not something you would usually replicate, I can't think of many other examples in the JVM where there is a paired marker interface and method. From Java 5 onwards it's better to use annotations for markers. e.g. the @XmlRootElement used to mark a type as Jax-B serializable (post Java 5) vs the Serializable interface (pre Java 5) used to indicate a class is binary serializable.

Sameera

What is a marker interface ?

Interface which does not contain any method to implement are called marker or the tag interfaces.

Why marker interfaces ?

The basic idea of having marker interfaces are to mention that the class is implementing an interface of which the behavior is implicit. The class is not expected to implement anything to adhere to the contract defined by the interface. In contrast it is to indicate the JVM regarding an expected functionality to perform implicitly.

Java Example

  • Serializable - Eligible to serialize this type of objects Remote - This type is eligible remote method calls Clonnable - Eligible to make a field-for-field copy of instances

Can we create custom marker interfaces ?

Yes, It is possible.

A marker interface is a common technique to tag classes. They don't add behaviour to classes (in general). The Clonable interface is such a tag: every class tagged with Clonable is able to clone itself (that's the rule).

Same with Serializable, although there is some more hidden magic behind that marker interface (the object serializer looks for some methods and fields, that the tagged class may implement or not)

Bonus info: forget about Clonable, its broken. If you want to create clones in real life, look for the copy constructor pattern.

Parth

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

More info available here

Citation From Josh Bloch's Effective Java:

"The Cloneable interface was intended as a mixin interface for objects to advertise that they permit cloning. Unfortunately it fails to serve this purpose ... This is a highly atypical use of interfaces and not one to be emulated ... In order for implementing the interface to have any effect on a class, it and all of its superclasses must obey a fairly complex, unenforceable and largely undocumented protocol"

The java.lang.object class is a super/parent class of all java classes, if you want to create an object in java ,then it should be implements java.lang.object class. If you are not importing Object super class in your code then compiler will implicitly import that in your code.. SO automatically all its properties and behavior is available for you are object(program) ,including clone() method, if you call clone() method in your program that mean clone() method is called from super class (Object class),not from child class.

Marker Interfaces: Its true Marker Interfaces are empty interface it does not contains properties and behaviors. Now, The Question may be Raised.

Q. Who will implement Predefined Marker interface, if used in our program?

Answer: JVM will take that responsibility because, inside JVM that Marker interface functionality is defined, so it implements and adding some advance functionality to you are program.

So programmer no need to implements Clonable Marker interface, JVM will take that responsibility.

Marker interfaces do not have any body as such.They just ask java interpreter to behave in a predefined particular way for objects of classes extending them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!