Cloneable Interface

Hi,
I know there have been lots of questions been posted for Cloneable being a marker interface .. but none off them has been satisfactory.
my question is
Clonable is a Marker interface. Object class has a protected method clone(). Now if we wish to override clone() method we must implement the cloneable interface but the cloneable interface doesnt define any such method.
So how does one determine inorder to overide clone() method as defined in Object class, we need to implement Cloneable interface.

'The method clone() for class Object performs a specific cloning operation', but that's the method clone() for class Object, isnt' it? There's nothing in the adjacent text or elsewhere to suggest that you can't or shouldn't implement a different behaviour, and plenty to suggest that you can.
In fact there are many permissible behaviours:
1. Do nothing. Object.clone() will throw CloneNotSupportedException.
2. Implement Cloneable. Object.clone() will perform a shallow copy.
3. Override clone(). Roll your own semantics.
4. Override clone() and implement Cloneable. You can call super.clone().
5. Override clone() and throw CloneNotSupportedException.
6. Override clone(), implement Cloneable, and throw CloneNotSupportedException.
Independently of all this you can also widen the access of clone() to public if you implement it.
There may be others. None of these is excluded by any wording I can see in the JLS or the Javadoc for Object.clone().
The reasons why clone() is protected not public, and is not defined in Cloneable, are 'lost in the mists of time' (Sun) but those who were around in 1997 will remember many discussions in which it was made clear that all the possible behaviours were intended to be supported. See the thread I quoted earlier.

Similar Messages

  • Doubt abt Serializable,Set,Cloneable Interface

    WE know that Serializable,Set,Cloneable Interface have no
    members.But by Implementing this the classes get some Features .
    example a class can get Serializability by implementing the java.io.Serializable interface.
    How it is Possible ? Tell me some Idea?

    WE know that Serializable,Set,Cloneable Interface
    have no
    members.But by Implementing this the classes get some
    Features .
    example a class can get Serializability by
    implementing the java.io.Serializable interface.
    How it is Possible ? Tell me some Idea?Serializable is just a "marker interface." It doesn't declare any methods. It's just a flag that other methods can check. DataOutputStream.writeObject checks whether the class implements Serializable, and throws and excpetion if it doesn't.
    Likewise Cloneable.
    For "normal" interfaces, like Set, you get the behavior by implementing the methods. For example Set specifies and add(Object obj) method. If you implement a Set, you must create and implement that method. If you don't, the code won't compile. What you do inside that method is entirely up to you. For it to be correct, its behavior must follow the interface's docs, but the compiler can't check that.

  • Cloneable interface extends Object Class ? T/F can anybody ?

    Can a Interface extend Class?
    If not so, then how come Cloneable Interface extends Objects Class.
    if so, how? can anybody help me out.

    Beats me how you made the jump to this example. Whats
    happening
    here is that when you initialize byte b with the
    integer literal 5, the compiler
    is smart enough to figure out that 5 is in the range
    of byte values,
    so it doesn't complain.I understand what is going on and I know it is somewhat of a stretch but it still feels the same to me.
    As you said, the compiler is smart enough to know that 5 will fit and does not complain; however, according to the JLS integer literals are ints.
    That makes the above example an exception to the casting rule similar to the casting exception between interfaces and Object. Although, the interface situation may very well be specified in the JSL - it might say that all interfaces are a descendent of Object (I can not remember - need to look).

  • Java.lang.cloneable

    Hi,
    I am using SunOne Studio ME. I have added an additional jar file to it and added it to my project. I get the followng error.
    Execution completed successfully
    11833 bytecodes executed
    12 thread switches
    327 classes in the system (including system classes)
    564 dynamic objects allocated (29192 bytes)
    4 garbage collections (6280 bytes collected)
    Total heap size 500000 bytes (currently 468896 bytes free)
    ALERT: Unable to load class java/lang/Cloneable
    Regards,
    Salil.

    thats because there is no java.lang.Cloneable Interface in J2ME. Only in J2SE. So you have to reimplement the contents of the jar in order to use it. No way around.
    hth
    Killer_2001

  • Regarding clonable interface

    why is it that we need to implement the cloneable interface ,if we need to clone an object of the particular class,Even though the interface don't contain any methods.?
    Thanks
    Matis

    Thanks for the reply...it has really helped me to get an idea abt it..

  • How to specify that a generic type should implement cloneable ?

    Hi everybody,
    I created a map type called StringMap as such
    class StringMap<V>{
    HashMap<String,V> map;
    and now, I want to implement the method clone() in my StringMap, for that I would like to use the method V.clone()
    but I get an error saying "The method clone() from type Object is not visible". This is logical since clone() is protected in Object class. So I believe a solution would be to specify that V implements the interface Cloneable(). This tried this:
    class StringMap<V implements Cloneable>{
    but it doesn't work.
    Do you know a way to do it ?
    Thanks

    dubwai wrote:
    r035198x wrote:
    You can say <V extends Cloneable> instead.That still won't solve the root issue. The Cloneable interface has no methods and implementing it will not expose clone(). Cloneable is broken.Yep, I was hoping the OP would pick that up after that.

  • MARKER INTERFACE WHY?

    hello i madhav new to this forum , i have doubt on marker interface . as it doesnot have any body why we r using that one instead of skipping.Is there any concept behind is there.More about my doubt is Take Example Cloneable Interface it Doesn't Have any Method In it,Clone() method is from Object class,then what is the necessity of implementing the interface .Why it should not be like that.
    what is real technolgy behind MarkerInteface and Why we r going to be used in what situations it will needed.And Lastly is there any method related to Serailizable interface if not what it do .
    marker interface can also called as Tag interface what is tag mean here just saying combing derived classes . i didn't get that one and how it will be .
    if u could explain with example or real time project it would be better for me
    thanks in advance

    What is a "marker" interface?
    Marker Interface pattern
    c2.com - Marker Interface
    experts.about.com - Marker Interfaces
    The Purpose of the Marker Interface
    Maximize your Design ROI with Marker Interfaces and JavaDoc

  • Marker interface use

    In which case Marker interface is getting use plz tell

    Marker Interfaces are giving specification to the JVM about the implementing classes.
    One of the features of the Java programming language is that it mandates a separation between interfaces (pure behavior) and classes (state and behavior). Interfaces are used in Java to specify the behavior of derived classes. Often you will come across interfaces in Java that have no behavior. In other words, they are just empty interface definitions. These are known as marker interfaces. Marker interfaces are also called "tag" interfaces since they tag all the derived classes into a category based on their purpose.
    Some examples of marker interfaces in the Java API include: -
    Java.lang.Cloneable
    A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.
    java.io.Serializable
    Serialization is nothing but s saving the state of an object to persistent storage as byte stream. Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable. To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.
    java.util.EventListener
    A tagging interface that all event listener interfaces must extend.
    java.rmi.Remote
    The Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface. Only those methods specified in a "remote interface", an interface that extends java.rmi.Remote are available remotely.
    Javax.servlet.SingleThreadModel
    Ensures that servlets handle only one request at a time. This interface has no methods. If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method.
    Java.util.RandomAccess
    This Marker interface used by List implementations (ArrayList, Vector) to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.
    Javax.ejb.EnterpriseBean
    The EnterpriseBean interface must be implemented by every enterprise Bean class. It is a common superinterface for the SessionBean, EntityBean and MessageDrivenBean interfaces.
    Reference From Sun Documentation

  • Class implements Cloneable?

    Is it possible for a class to implement Cloneable in JavaME - as I can't seem to get it to work.
    Thanks

    No, the Cloneable interface isn't part of Java ME. You could have found that out yourself by checking the Javadocs for JSR-118 and JSR-139.
    db

  • A Question regarding use of Interfaces.

    I have read about interfaces,but I havent got the real grasp or point
    of it.
    1). How is an interface an improvement over classes?
    If I define a class which has several important methods in it,and this class is extended by several other classes,and all these sub classes then call the super class's methods.
    How does an interface be more beneficial?
    2) What is the exact use of creating class intances thru interface references?
    Java talks about multiple inheritance using interfaces.
    If an interface does not have any constructive methods,but just
    empty methods,how does this become more constructive?
    Eventually a class will implement the this interface to give it some
    value? So why dont I write a class straight away rather than creating an interface,implement it and then code the required methods defined.
    I tend to avoid using interfaces becos I find them of no value.
    Rather I wud prefer to define class with some constructive methods
    and extend this class.
    Am I right in my analysis?

    Interface is also a "contract" that the class implementing a particular interface will provide certain functionality to the users of the class. For E.g Serializable interface in Java. All the classes that implement this interface indicate that the objects of these classes are serializable. Similar thing with Cloneable interface, all objects of classes that implement this interface are Cloneable....
    Hope thinking in these terms will help your understanding. Just look at Java interfaces to understand instead of some custom code.

  • How marker interface works?

    Marker interface have blank body containing nothing.
    I want to know how implementing class start behaving in that manner or how marker interface technology work??

    Marker interfaces are also called "tag" interfaces since they tag all the derived classes into a category based on their purpose. For example, all classes that implement the Cloneable interface can be cloned (i.e., the clone() method can be called on them). The Java compiler checks to make sure that if the clone() method is called on a class and the class implements the Cloneable interface. For example, consider the following call to the clone() method on an object o:
    MyObject o = new MyObject();
    MyObjectref = (MyObject)(o.clone());
    If the class MyObject does not implement the interface Cloneable (and Cloneable is not implemented by any of the superclasses that MyObject inherits from), the compiler will mark this line as an error. This is because the clone() method may only be called by objects of type "Cloneable." Hence, even though Cloneable is an empty interface, it serves an important purpose.

  • Creating a copy of an existing JTable that does not have source code.

    Hi
    I am working on a java swing based application. We used a third party API for JTable for showing some data in a table format and also for printing function. As of now print button is printing the whole table being shown in the application. Our new requirement of printing is �Users don�t want to print all the columns being shown in the application and they wanted to have print of columns whatever they would like to see�.
    The above requirement can be fulfilled if I can have a copy of the existing table. Unfortunately we don�t have the source code of the table being shown in the application as we are using 3rd party libraries. Had the source code been there I could have implemented cloneable interface or proto type pattern.
    Can some body help me how can I create a copy of the existing table? Basically the high level approach I thought of is create a new object of the table class and copy the properties/attributes of the existing table to the new table object. But I don�t know what attributes should be copied so that I can have the copy of the original table and how to construct (code) this new table.
    I would be very grateful if some one could help me with this.
    Thanks & Regards
    Srini

    I understood your suggestion. But it would be very difficult to migrate as it requires lot of changes across different tabs of the existing application. This application was developed around 6 years ago. And this change would require lot of regression testing too.
    Do you have any idea of creating the copy of the available JTable object?

  • Is the statement in JDK 1.4 Doc is misleading ??

    hi all,
    I read the following information in standard JDK 1.4 Doc
    public interface Cloneable
    A class implements the Cloneable interface to indicate to the
    Object.clone() method that it is legal for that method to make a
    field-for-field copy of instances of that class.
    Invoking Object's clone method on an instance that does not implement the
    Cloneable interface results in the exception CloneNotSupportedException
    being thrown.
    By convention, classes that implement this interface should override
    Object.clone (which is protected) with a public method. See Object.clone()
    for details on overriding this method.
    Note that this interface does not contain the clone method. Therefore, it
    is not possible to clone an object merely by virtue of the fact that it
    implements this interface. Even if the clone method is invoked
    reflectively, there is no guarantee that it will succeed.
    ok..
    After that I am executing the following program.
    class over5 {
    T t=new T();
    public int a = 100;
    over5(int x){
    a=x;
    over5(T t,int x){
    this.t=(T)t.clone();
    a=x;
    protected Object clone() {
    return this;
    class over12 {
    public static void main(String as[]) {
    over5 o = new over5(5);
    over5 o1 = (over5)o.clone();
    System.out.println(o1.a);
    class T{
    protected Object clone() {
    return this;
    This code is working without implementing the clonable interface.
    My question is the java program that using cloneable interface is somewhat different or Is there any misleading statement in Java DOC
    Pls help me ...
    I really confused
    regards,
    namanc

    What is a "marker" interface?
    Marker Interface pattern
    c2.com - Marker Interface
    experts.about.com - Marker Interfaces
    The Purpose of the Marker Interface
    Maximize your Design ROI with Marker Interfaces and JavaDoc

  • Trying to Clone an array object that contains other objects

    I have an employee object with the fields name (String), Salary (Double), and hireDay (Date). Since the date field contains many int fields (year, month etc..), using a straight clone will not work.
    What I want to do is copy an array of employees into a new array.
    For example:
    Employee [] newEmps = (Employee[])OldEmps.clone()
    In the employee class, I implement the Cloneable interface and my code so far will copy one Employee. How do I change this to copy an array of empoyees? Specifically, since hireDay contains many fields, a straight super.clone of the array won't work. Here is my clone code:
         public Object clone()
         try {
              // call Object.clone()
         Employee cloned = (Employee)super.clone();
              // clone mutable fields
    cloned.hireDay = (Date)hireDay.clone();
         return cloned;
         catch (CloneNotSupportedException e)
         { return null; }
    Any help is appreciated. Thanks!
    -Eric

    There's so much I don't understand about that, it just
    isn't worth asking. Let me spell out my previous post
    since it seems to have gone right over your
    head.Employee[] emps;
    // here you fill in the array of Employees
    Employee[] clonedEmps = new Employee[emps.length];
    for (int z=0; z<emps.length; z++) {I understand this next line of code. My question lies within the clone() function
    clonedEmps[z] = emps[z].clone();OK - Let me try to clear things up. If we take from your example the original array "emps". Now for me, emps, is already filled up in main().
    So in main() when I call emps.clone(), my clone function starts running:
    // the employee class (which I created) implements the Cloneable
    // interface
    // This clone function is inside my employee class
    public Object [] clone()
    try {
    Employee [] cloned = (Employee)super.clone();
    // IS THIS NEXT LINE OF CODE REQUIRED TO MAKE A DEEP COPY? IF SO, How would I implement perhaps a for loop that would copy the hireDay from each element in the original array to the elements in the the new array?
    // Obviously, each employee (element in the array) has a hireDay
    // Please modify this next line of code to show me how.
    cloned.hireDay = (Date)hireDay.clone();
    return cloned;
    catch (CloneNotSupportedException e)
    { return null; }
    If I wanted to access say the "hireDay" field of the 3rd element in the original employee array INSIDE THE CLONE FUNCTION - then how would i access it?
    Thanks
    -Eric

  • For what is used the Serializable?

    Hi! I'm wondering for what can I use the Serializable interface. Since it do not have methods or fields. Can you tell me for what it is used and provide a little example of it?
    Thanks a lot!
    Raul

    Please tell me if the interface Serializable is having
    no methods and variables. Then whats the use of this
    kind of interfaces...
    Marker Interface Pattern
    The marker interface pattern is a design pattern in computer science.
    This pattern allows a class to implement a marker interface, which exposes some underlying semantic property of the class that cannot be determined solely by the class' methods. Whereas a typical interface specifies functionality (in the form of method declarations) that an implementing class must support, a marker interface need not do so. The mere presence of such an interface indicates specific behavior on the part of the implementing class. Hybrid interfaces, which both act as markers and specify required methods, are possible but may prove confusing if improperly used.
    One good example of a marker interface comes from the Java programming language. The Cloneable interface should be implemented by a class if it fully supports the Object.clone() method. Every class in Java has the Object class at the root of its inheritance hierarchy and so every object instantiated from any class has an associated clone() method. However, developers should only call clone() on objects of classes which implement the Clonable interface, as it indicates that the cloning functionality is actually supported in a proper manner.

Maybe you are looking for

  • Delivery Completed Indicator for Services

    Hi Guys In PO's, in the delivery tab, the Delivery Completed Indicator appears only for Stock PO's. Is it possible to make the Delivery Completed Indicator appear for non stock PO's as well, I mean for services? Thanks

  • Reverse the sort output for autofeed copying??

    is there a way to "reverse" the output for copying when using the autofeed tray? i sometimes copy 20+ page documents and every time i do this i get the first page face up and then the second page comes out face up and then the third so that i have to

  • Alias hotmail.fr and iphone

    Hi everybody ! I would like to use an alias adress with the app Mail and I'm getting in trouble : - I have a parent adress "[email protected]" - and an alias "[email protected]" I've tried to configure with pop or exchange protocols but failed ( with

  • Pop up windows appear when using RP Data but the window is empty IE no data is displayed. How can I fix this?

    The real estate information web site, RP Data, has a mapping feature that should display a site map of the property being searched in a pop up window and it is this particular pop up that doesn't work; other pop ups work EG Google Street View. Please

  • New subscription; can't call phones

    I just renewed a subscription yesterday, phone was working up until just moments ago.  I keep getting, subscription expired, however, I go to my account and it's active.