Two Laptop Bag*
The moose likes Java in General and the fly likes why can't we call clone() method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why can Watch "why can New topic
Author

why can't we call clone() method?

budsy remo
Ranch Hand

Joined: Sep 20, 2008
Posts: 103
The clone method is protected in Object class and each class created in java extends Object class. Now if we create a class and declare a method as protected then it's subclasses should be able to instantiate a base class object and call it.
Example:-


But if we try to use the clone method it gives me an error that clone has protected access .why?
example:-

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Subclasses can access a protected method via inheritance only (if they're in a different package, which they are in this case). That means, you can access it from within the object that is inheriting it, but not from outside that object (even if it's within the same class).

So, for instance, this is allowed:but what you're doing there is not.



Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The semantics of "protected" are subtle, and a lot of people don't quite understand them. To say that a protected method may be called by child classes is actually not quite right. A protected method of a parent class may be called by code in a child class on a reference whose type is at least that of the child class. In other words, if B extends A and A extends Object, then code in A can call clone() on instances of A or B, but not on instances of Object itself.

The reason that your first example compiles is because both A and B are in the same package; within a package, all classes may call the protected members of all other classes, regardless of inheritance. If you put A and B into different packages, the example would not compile, just the same as the A/Object example.

Regarding the clone() method itself: to make clone() generally usable on a class, you must override it and make it public; you must also mark the class with the "Cloneable" interface. That's simply how clone() is designed -- most folk these days think it's a bad design.

[ Edit: too slow!]


[Jess in Action][AskingGoodQuestions]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

In addition, once you've got round that you'll hit a second problem. If you look up the Javadocs for the clone method, you'll see that it will throw, by default, a CloneNotSupportedException if the object doesn't implement Cloneable.

The convention is, to make a class you can clone, you should do the following:
- implement Cloneable
- override clone() with a public version (allowing you to clone it from outside)
- either call super.clone(), or do all the cloning yourself (or a mixture), depending on the exact requirements.

Look up the Cloneable interface definition for more details.
budsy remo
Ranch Hand

Joined: Sep 20, 2008
Posts: 103
Thanks a lot ! now i get it!
 
Don't get me started about those stupid light bulbs.
 
subject: why can't we call clone() method?
 
Similar Threads
inheritance question
generics doubt
simple........
Shallow Copy implementation Question
[ Protected methods ] Don't understand error message