3

When i implement Cloneable in a class, say class AA, class AA is cloneable-- i can invoke clone() on an object of it without getting a CloneNotSupportedException. i can do this
without overriding clone() (although it is recommended in order to keep the object independent from its clone).

When a class is Cloneable, so are its descendants.

In the following code,

class ZZ {protected void mm(){System.out.println("+++");}}

class AA extends ZZ implements Cloneable {    
    // ...
    protected Object clone () throws CloneNotSupportedException {return super.clone();}
    AA cloneMe() throws CloneNotSupportedException { return (AA)clone(); }
}

class C extends AA  {
    public static void main(String[] args) throws CloneNotSupportedException {
        C c = new C();        
        AA a = new AA(); 
        AA a22 = (AA)(c.clone());   
        AA a3 = a.cloneMe();   
        C c2 = (C)c.clone();  

        AA a21 = (AA)a.clone();   
        a.mm();
    }
}

i'm getting a checked error that

clone() has protected access in Object

to the line

    AA a21 = (AA)a.clone();   

when i comment the implementation of clone() in AA, i.e., the line

   // protected Object clone () throws CloneNotSupportedException {return super.clone();}

Since AA is cloneable, i should be able to clone an instance of it without implementing clone() in AA itself. AA is inheriting clone() from Object.

So - how come this error???

TIA.

//=======================================

EDIT:

I have the same issue with the code above except class ZZ which is the following code:

class AA implements Cloneable {    
    // ...
    // protected Object clone () throws CloneNotSupportedException {return super.clone();}
    AA cloneMe() throws CloneNotSupportedException { return (AA)clone(); }
}

class C extends AA  {
    public static void main(String[] args) throws CloneNotSupportedException {
        C c = new C();        
        AA a = new AA(); 
        AA a22 = (AA)(c.clone());   
        AA a3 = a.cloneMe();   
        C c2 = (C)c.clone();  

        AA a21 = (AA)a.clone();   

    }
}

class ZZ was there to show that the invocation of a protected method by a "grandchild" doesn't give the same error as the one by an invocation of clone() in a similar way.

//===============================================

EDIT-2:

The answer in super.clone() operation not works in Derived Class says

"Clone is one of the early designs in java and it has flaws".

this seems to be the only explanation. looking for further verification on this.

i've looked into this before and without success-- some more on the native methods than their explanations in the Java docs. Still-- is there any more to the internals of clone()-- how it is implemented, how it clones the members etc as well as how it is "managing" this out-of-the-ordinary access privilege? and under the circumstances??

Community
  • 1
  • 1
user3880721
  • 613
  • 6
  • 16
  • [I get no such error.](http://ideone.com/LsDp1S) – user2357112 Aug 26 '14 at 19:31
  • 2
    The root cause here seems to be a misunderstanding of the `protected` access modifier. If your question is about how `clone()` is natively implemented, please rephrase your question or open a new one. – Sotirios Delimanolis Aug 26 '14 at 19:33
  • @SotiriosDelimanolis: All uses of `protected` methods are from classes that have access to the methods, at least in the code shown. – user2357112 Aug 26 '14 at 19:35
  • @user2357112 No, `C` does not have access to `AA`'s `clone` on an `AA` reference. – Sotirios Delimanolis Aug 26 '14 at 19:37
  • @SotiriosDelimanolis: Huh. You seem to be right, according to the JLS, but neither Ideone nor my own copy of Eclipse have a problem with it. Dunno why. – user2357112 Aug 26 '14 at 19:40
  • 1
    @user2357112 Because your classes may be in the same package, at which point they are dealt with differently as rules of default access precede `protected`. – Sotirios Delimanolis Aug 26 '14 at 19:41
  • Ah, of course! I copy-pasted the example into a single file, but the questioner's real code wouldn't be structured like that. – user2357112 Aug 26 '14 at 19:43
  • @user2357112 Also note that OP gets the error when they comment out the overriden `clone` method in `AA` (which is in the same package). – Sotirios Delimanolis Aug 26 '14 at 19:44
  • Effective Java Second Edition warns you about clone in "Item 11: Override `clone` judiciously". It recommends using a copy constructor or (static) copy factory instead. Note that you'd have to write these manually... but you're already doing that if you're writing your own clone method. – Powerlord Aug 26 '14 at 19:44
  • @Powerlord thx for a useful comment at last. will look at the Item although it didn't show an indication of an answer to this at first glance. – user3880721 Aug 26 '14 at 19:51

0 Answers0