• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
shreyas Kulkarni
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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?
 
Daniel Croft
Greenhorn
Posts: 21
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!