• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

understanding access modifiers

 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have this thread:
https://coderanch.com/t/395617/java/java/first-interface
where access modifiers publicand private appear.
I understand the abstract meaning of each one; but what i wanna get is when and why we use them, actually in the code in the page i refer to.
In other words: does one need to say public/private because its necessary or its just a question of elegance?
(remember i mean in my code)
thanks in advance
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In answer...
The use of access modifiers is never a question of elegance, but of correctness.
Although, it's fair to say though that if you are writing a small program that is for your very own personal consumption that you might just go "What the heck" but that would be actually quite tardy and bad form and a programmer should always be aware of the implications of the code they are writing.
When writing your classes which define the set of objects you will be using in your programs you need to give thought as to who will be using your objects and how will they be using them.
Accessors are there to provide access security to your objects. Your objects naturally define data and methods that work on that data and allowing anyone (or object) unfettered access is going to cause bugs.
For example, you may define a class that other people may use in their code. If you make a method public (lets call it foo() for now) then you have set up an implicit contract with the consumer of your class. You have exposed behaviour that generally you must then continue to provide. Further if the method is not final then consumers of your class might be able to modify that behaviour somewhat in their subclasses by redefining foo() - and if you as the original base class designer were relying on your implementation of foo() to do something in a certain way, or modify state in a certain way then you are now at the mercy of the client programmer who has redefined your method as Java is late binding and you'll call through into their subclass.foo() implementation.
Generally lock down access to your object as much as possible. Its easier to provide a feature later to a consumer (or yourself) then to take it back again because "once public, forever public" is a common mantra in the commercial world...
[ February 21, 2004: Message edited by: Jason Kingsley ]
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did understood now
thanks a lot
 
reply
    Bookmark Topic Watch Topic
  • New Topic