• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Redundant Access Modifier?

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there!
Well I just looked at the clone() method of the Object class. Now its access modifier is protected. But since every class is a subclass of Object then every class can access this method. So why not just make it public?
What am I missing?
Dan
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question - here's what I came up with. Say I'm writing a class MyClass, and I want to be able to create copies of my instances, but I don't want anyone to use clone from outside MyClass. I might actually prefer that clone() were private - but if I want to use the default provided by Object, I obviously need to be able to inherit it. So protected is the most restrictive modifier I can have for this method. Now if I do in fact want other classes to be able to access my clone() method freely, I can certainly override clone() to be public in MyClass - but if it had been public in Object, then there would be no way I could make it more restrictive. This way at least I have a choice - I can make the method public or protected. It's not much of a choice, but it's something.
 
Dan Temple
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay - that makes sense. Thanks Sheriff!
Dan
 
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic