Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why java.lang.Object can not be cloned?

Tags:

java

clone

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.

like image 310
amicngh Avatar asked Oct 28 '25 13:10

amicngh


1 Answers

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.

like image 57
Louis Wasserman Avatar answered Oct 30 '25 03:10

Louis Wasserman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!