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