Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

protected clone() doubt

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I was reading about clone() from Thinking in Java.
TIJ3

There is an example (given below) which gives a compile time error that says clone() is not accessible.
Integer x = new Integer(1);
x = x.clone();

My question:

1. Why is not clone() accessible. Every class in Java automatically extends java.lang.Object which means that protected methods such as clone() should be accessible.

regards,
vijay.
 
Marshal
Posts: 75702
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome on board.

I am not sure why you are having problems, but why are you setting x to be a clone of itself?

CR
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was just an example. You can take any other class which does not override clone(). Click on the link in my first post, you'll see the example there.

The clone method() in java.lang.Object copies bit by bit. Its protected there. So any class in the same java.lang package and any other class that inherits java.lang.Object can access the clone() method. Then why do we get an error saying that clone method is not accesible.

regards,
vijay.
 
Campbell Ritchie
Marshal
Posts: 75702
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not an example. It says in your link it won't work.
 
Campbell Ritchie
Marshal
Posts: 75702
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another thread on this forum, active today, where there is more discussion about clone() being protected.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to implement the Cloneable interface in your class with the object you wish to clone.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your question is more about protected access than about the clone
method itself. As far as class Integer is concerned, Method clone is just an
another example of a proteced method. So, we could as easily use the following
example:Your question then is why can't you do the following:But do you understand why that line violates protected
access if it is written in a class outside package widget?

Going back to Integer, one could also ask, "why isn't the clone method in
Integer public?". (The original post was rather vague and it's not clear
what was really being asked there.) Well, Integer is an example of an
immutable class, like String, so there is no reason to clone instances!
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic