Henry Wong wrote:the Cloneable interface -- which has one method, the clone() method.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:No it doesn't. The clone() method is declared in Object as protected. Cloneable does not have it, because that would mean that the clone() method of any class that implements Cloneable would need to become public. For some reason Sun decided it should be possible to keep it protected.
No.Arun Singh Raaj wrote:So @henry, you mean to say that cloneable interface has a clone() method but implementation has been done in Object class?
Campbell Ritchie wrote:
No.Arun Singh Raaj wrote:So @henry, you mean to say that cloneable interface has a clone() method but implementation has been done in Object class?
There is a rudimentary implementation in the Object class, but you cannot simply clone an Object because its clone() method has protected access and is therefore inaccessible outside the java.lang package.So what you do is to override the clone() method with public access and also implement the Cloneable interface, which, as you can see from this link, is a tagging interface with no methods. You must do both.
If you find a copy of Effective Java by Joshua Bloch you can find more about the clone() method (page 54) and you will find a different opinion about marker/tagging interfaces (page 179). You will see that Bloch warns that the clone() method is difficult to use.
Campbell Ritchie wrote:Welcome to the Ranch
Have you got a question?
I have a question for u.Since every class extends Object class,why can't it access the protected clone methodCampbell Ritchie wrote:Welcome to the Ranch
Have you got a question?
thanks a lot.Campbell Ritchie wrote:Don't use abbreviations like “u”; they are difficult for non‑English‑speakers to understand.
You want to be able to call myObject.clone() from outside the class. That won't work because the protected modifier permits access within the same package or in code responsible for the implementation, which may be in a subclass.Try this:-The only way you can get such a method to execute is to override it with public access.
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|