What will be printed as a result of compiling and running the following code?

public class Test {
    public static void main(String[] args) throws Exception {    
        A a = new A();
        a.setI(4);
        if (a instanceof Cloneable) {
            A a2 = (A) a.clone();
            System.out.println(a.getI() == a2.getI());
        }
    }
}

class A implements Cloneable {    
    private int i;
    
    public void setI(int i) {
        this.i = i;
    }
    
    public int getI() {
        return i;
    }
}
Explanation
The error is related to the absence of the access to the clone method. It's a protected for the class Object.
And the Cloneable interface contains no methods to be redefined.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes