Domanda

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 ?

È stato utile?

Soluzione

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();
    }
}

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top