可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Could somebody pls explain the contract of marker interfaces in java?
For Ex: If Clonable
is a Marker Interface with no fields/methods, then where is the clone()
defined?
Why should we implement Clonable
i/f whenever clone()
is used?
Well my question was, if clone()
is a method of java.lang.Object
class, why implement Clonable
i/f to override clone()
.
Could somebody elaborate on this convention of java ?
Thanks in Advance
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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"
回答5:
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.
回答6:
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.