When i try to clone a generic Object i get compile time error . why?
Object obj=new Object();
obj.clone(); // Here compile time error "The method clone() from the type Object is not visible"
Every class extends Object class and clone method is protected in Object class.
protected methods can be accessed in same package as well as by subclasses and all classes are child of java.lang.Object.
Because clone is protected in the Object class. It's not public.
The only way to get access to an object's clone() method is to know it has a compile-time type that has a public clone() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With