Java – tag interface

Can anyone explain the contract of marking interface in Java?

For ex: if clonable is a tag interface without fields / methods, where is the definition of clone()?

Why implement clonable I / F whenever clone() is used?

My question is, if clone () is Java Lang. object class, why should we implement clonable I / F to override clone()

Can anyone elaborate on this Java convention?

Thank you in advance

Solution

Clone() in Java Lang. object class. All classes extend from, but it is protected This is actually a concrete method implementation that executes fields by cloning the fields of the object, but only if you have implemented the clonable interface to indicate that this is allowed

In practice, many people override clone () methods so that they can be exposed and allowed to be cloned from outside the class

This whole pattern is unusual and usually not replicated. I can't think of many other examples of pairing tag interfaces and methods in the JVM Starting with Java 5, it's best to use annotation tags For example, @ xmlrootelement is used to mark the type as jax-b serializable (post Java 5) and serializable interface (pre Java 5), indicating that the class is binary serializable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>