java: clone method violation
Asked Answered
B

2

8

Code behind:

class A implements Cloneable
{
    int i, j;

    A(int i, int j)
    {
        this.i = i;
        this.j = j;
    }

    A()
    {
    }
}

class B extends A
{
    int l, m;

    B()
    {
    }

    B(int l, int m)
    {
        this.l = l;
        this.m = m;

    }

    public static void main(String l[])
    {
        A obj = new A(1, 2);
        B obj1 = (B) obj.clone(); // ERROR
    }
}

I know that I am violating the meaning of clone as I am trying to assign the fields of one object to a completely different object. But its the error statement that is confusing me.

Statement: "error: clone() has protected access in Object"

Extending A should make clone() available to B also ? If that is, so values of i and j should be copied to l and m also ? Is this possible ?

Burglary answered 22/9, 2012 at 7:34 Comment(0)
T
8

clone() is protected method and to make accessible in sub-classes, override it with public access.

class A implements Cloneable{
    .....
    @Override
    public Object clone() throws CloneNotSupportedException{
      return super.clone();
    }
}
Thorsten answered 22/9, 2012 at 7:36 Comment(2)
if clone() is protected then it is available to A and if B extends A then B should have access to clone ?Burglary
@rd4code See my answer. B has access to the clone method. But B should access it through inheritance and not through A directly.Tarentarentum
W
3

From Javadoc of Cloneable

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.

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

About access- When a method is protected, it can only be accessed by the class itself, subclasses of the class, or classes in the same package as the class.

So it is accessible in Aand B classes the way you are doing it is only possible if you are in the same package that happens to be java.lang

You can provide some method like this inside A.

public A copy() throws CloneNotSupportedException {
        return (A) clone();
    }

Correct Implementation

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    };

Also Remember parent is not type of child so casting from A to B will not work. Child is type of Parent so casting from B to A will work.

Whitening answered 22/9, 2012 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.