9

This is not a literal coding question, I'm just wondering, what's the point of using public and private functions and variables? Where would I use each of them?

I've always thought of them as being "old" or not needed anymore, but I'm sure there's a good reason they're used in some places.

Kavi Siegel
  • 2,964
  • 2
  • 24
  • 33

3 Answers3

14

Basically, when you are developing a class that other developers will use :

  • public methods and variables are what the other developers will (need to) use -- what your class does
  • private methods and variables are what you, as the developer of that class, used to make it work -- it's how your class works internally.

Others need to be able to use your class ; but not to know how it does it.


If you want to know more, you'll have to search for Encapsulation.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • So basically, if I plan on distributing my code, they distinguish between functions somebody else would use and functions that the class uses with its self? – Kavi Siegel Mar 21 '11 at 16:17
  • 1
    Even if you don't distribute your code : anyone who uses your class *(even yourself)* know they can use its interface *(the public methods)*, that they will always be there, even if you modify the inner workings of the class *(the private methods)* – Pascal MARTIN Mar 21 '11 at 16:34
  • I see, I see. But there's no functional difference in declaring it, correct? – Kavi Siegel Mar 21 '11 at 19:45
  • 1
    except that only public properties/methods can be used from outside the class ? no. – Pascal MARTIN Mar 21 '11 at 20:00
2

Private is anything that has to do with the inner-workings of our class and should not be touched (left sanitized). For instance, maybe a path to a directory or the path to a web service, which has no use to whomever uses the class, but depends greatly on how the class operates.

Public is typically anything you'd like to expose to whatever is using the class such as a generated value, a username/password configuration--something that (though the class has set [generic] functionality) may be specific to that instance or run-time.

Think of it like running a business; You may provide a service to your customers that they understand what information is necessary of them for you to operate (public information). Supplemental to that, you need information of your own (vendors, pricing, etc.) that you don't want them to know about, but would need to function properly (private).

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
1

If variable or method is private you cannot access it from outside an object. This way You (as programmer) will not use it directly, what lets creator of an object oriented library to change its interior. And those changes are transparent to your code, becouse changes touch only private elements, public API stays the same.

Another profit is what we call encapsulation. If data is inaccessible (private) from outside you have to ask object to do things for you (throu its public API) instead of reaching for its data. It helps to use object in the way they where designed to use what enables to build larger more stable systems.

smentek
  • 2,820
  • 1
  • 28
  • 32