• 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

Getter and Setter Methods!!

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
could anyone please explain in simple way why the Getter and Setter methods are designed to get an access to attributes instead of making those attributes public and accessing them directly?
I would appreciate if you also could forward me a link that talks about concepts like this.
thanks.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Namaste Sathi:
hi all,
could anyone please explain in simple way why the Getter and Setter methods are designed to get an access to attributes instead of making those attributes public and accessing them directly?
I would appreciate if you also could forward me a link that talks about concepts like this.
thanks.


The short answer is encapsulation -- a core concept of Object Oriented programming. If only methods can access your private data, then you can do a variety of things:
  • Change underlying data structure
  • Delegate the access to another object
  • Instrument the access or perform other checks
  • etc.


  • Oh, and having accessor methods allows you to synchronize reads and writes to the data so you can prevent race conditions.
    [ June 07, 2003: Message edited by: Richard Jensen ]
     
    author
    Posts: 14112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have created a new FAQ entry for this topic: http://dirkschreckmann.dyndns.org:4000/JavaRanchFriki/view?AccessorMethods
    Please discuss!
     
    Ranch Hand
    Posts: 580
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's great Ilja !
    Another point is that accssors (as opposed to direct variable access) seperate implementation from interface. All clients should care about is that an object can provide a value not that it is internally implemented as an instance variable, a database call or whatever.
    D.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic