Reputation: 65
There are lot's of questions on this platform regarding clone() method of Object class. Everyone is answering different. There are lots of question regarding why clone() is protected. Some says..
- clone() is protected, so we have to provide our own clone() and indirectly call Object.clone() from it.
- clone() is protected soo outside to java.lang package we can access this method in subclass directly or with subclass Object only.
- clone must not be called on non-cloneable objects, therefore it is not made public.
Now Here are my questions.
Why Object.clone() dosen't have body part.
protected native Object clone() throws CloneNotSupportedException;
Upvotes: 1
Views: 1960
Reputation: 719386
If the above 2nd point is correct then how we can access clone() in subclass directly.
It is correct.
Like this:
public class Foo implements Cloneable {
public Foo copy() {
return super.clone(); // or just `clone()`
}
}
The point is that a method of a class can call protected
methods in its superclass. Note that Foo
needs to implement Cloneable
if you want to enable the default implementation of clone()
in java.lang.Object
. But you don't have to enable it, and you don't have to use it even if it is enabled; see below.
What are non cloneable objects.
They are objects that you can't call clone()
on (successfully). The exact meaning depends on the context:
It could be that objects that don't have a public
clone method, and you cannot call the protected
one.
It could be that the (non-public) clone method does not work; e.g. because the class doesn't implement Cloneable
or extend one that does.
What is the need to override
clone()
in subclass in case of cloning. Whereas the protected member of another package we can access inside another package by inheriting its parent class directly.
There are three distinct reasons for overriding clone()
:
To make clone()
accessible to other classes that wouldn't be able to access it otherwise. That is, any classes that are NOT in the same package and do NOT extend this class.
To alter the behavior of the clone()
method:
super.clone()
. You can implement the cloning behavior anyway that you like.super.clone()
.To override the return type so that you don't need to cast the result of clone()
. (The return type of Object::clone
is Object
.)
Why Object.clone() doesn't have body part.
Did you notice the native
modifier? Native methods do not have a body.
The cloning behavior of java.lang.Object
is implemented in native code. It is doing things that cannot be done in ordinary Java code. (Or using Java reflection, for that matter.)
Upvotes: 3
Reputation: 1803
protected native Object clone() throws CloneNotSupportedException;
This is the prototype of clone method in Object class
why no Body ?
It is a native method so it doesn’t have the body cause it’s body is provided by native library which is written in c language same as Jvm Or we can say we just need to give instruction to the jvm that we need to clone and the rest is taken care by Jvm
Why protected why not public?
Now clone() method is given as protected not public so that we couldn't clone Object class object
This behaviour provided to the sub classes or the classes created by the users .And we know that protected members are accessible outside the package inside Subclass only and on subclass object only.
why override?
We override it in subclass because there is a possibility that we need to call clone method outside the class and because of protected Behaviour we won’t be able to do that, so we override it with public modifier …prototype mentioned below
public Object clone() throws CloneNotSupportedException;
why object need to be cloneable?
Since the cloning object is done by Jvm so there jvm need a information like on which object cloning is required and for that object is required to have cloneable signature , And for that we need to implement cloneable interface which is a marker interface and it will inform to jvm that this specific object need cloning
Upvotes: 1