I know that there is a clone() method in Object class which is declared as protected, so that means I can call clone() in my own class since every class inherits from Object class, for example :
public class CloneTest
{
public static void main(String args[])
{
Employee employee1 = new Employee(...);
Employee employee2 = employee1.clone(); // but here has 2 errors
}
}
class Employee
{
...
}
first error is something about “access protected in Object”
second error is “incompatible type”
Why these errors happen ?
The clone method returns an Object if not overridden. So you must cast the result :
The first error is related to the content of the Employee class, that we don’t see. Does it override the clone method ? It should, that’s the condition to have it accessible from other classes that the called class.