• 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

Why data members are private when we can access them through getter/setters?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure whether I am writting at correct place or Not:-

Can anyone answer me, Why data members are private when we can access them through getter/setters?. why we dont make it public then?

-I gave answer like, if you want to validate your data, like adding some condition kinda, you can do it by adding just that code in getter and it will reflect everywhere but if it is public we have to change code everywhere..
And same for setter we can have validation like not greater or less than this etc....
But interviewer was still not happy...
Any other addition to my answer?
 
Greenhorn
Posts: 21
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a security precaution, the idea being that you can restrict the way that those variables are used. For example, you could have a private field with only a getter and no setter; this would stop people from setting the variable to null. If it's not a private field, then it can be set to null by part of the code with appropriate access.
 
shreyas Kulkarni
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya but I guess that also will not be the only reason, because its quite understandable when I said him(interviewer)its for validating data. so we can validate means eg: not allowing users to set value to null. I am not saying what you said is wrong but is there anything else with this?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's also a matter of separating interface from implementation. A getXyz() may happen to simply return the xyz member variable today, but tomorrow it doesn't have to. It might instead perform some calculation and not even store xyz at all. Additionally, though we don't do this often, a child class can override get/set methods, but it can't override direct access to a public variable.

And, finally, there are cases where a class is just a data holder with no behavior where we might decide it is okay to make the fields public.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shreyas Kulkarni wrote:Why data members are private when we can access them through getter/setters?. why we dont make it public then?


Because evil things will happen and worms will grow in your code.

Actually, there are two questions there:
Why are data members private?
and
Why can we access them through getter/setters?

The answer to the first is based in the principle of data hiding: Don't give anyone access to anything that they can screw up.

The answer to the second is not quite so simple; indeed, once you get past the basics you'll discover that you DON'T want getters and setters for everything in your class. This is a fairly ancient paradigm based on making everything a Bean, but other than display-side Web stuff (and possibly GUIs), I would advise against it. Classes are there to perform a function, so write methods that allow your users to interact with them, not set or get specific attributes that you really shouldn't have to know about. That's procedural thinking.

If you're interested, this is a good article on the subject. It doesn't have all the answers, but hopefully it'll get you thinking.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic