1

I don't know why the following program is generating this error-> clone has protected access in Object. Can't the derived class access the protected methods of the Object class? If I skip this statement cloned.id=(String)id.clone(); the program compiles successfully!

class Example implements Cloneable{
String id;
void display(){
    System.out.println("ID="+id);
    }
public Example clone() throws CloneNotSupportedException{
    Example cloned;
    cloned=(Example)super.clone();
    cloned.id=(String)id.clone();               //troublemaker
    return cloned;
    }
}
class CloneInterface{
    public static void main(String args[]){
        try
        {
        Example e1=new Example();
        e1.id="a";
        Example e2=e1.clone();
        e2.id="b";
        e1.display();
        e2.display();
        }
        catch(CloneNotSupportedException e)
        {
        e.printStackTrace();
        }
    }
}

0 Answers0