Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public vs. Private?

I don't really understand why it's generally good practice to make member variables and member functions private.

Is it for the sake of preventing people from screwing with things/more of an organizational tool?

like image 788
John Smith Avatar asked Feb 21 '12 22:02

John Smith


People also ask

What is the difference between a public and a private?

Public sector organisations are owned, controlled and managed by the government or other state-run bodies. Private sector organisations are owned, controlled and managed by individuals, groups or business entities.

Is it better to go public or private?

IPOs give companies access to capital while staying private gives companies the freedom to operate without having to answer to external shareholders. Going public can be more expensive and rigorous, but staying private limits the amount of liquidity in a company.

What are two difference between public and private companies?

Differences Between a Private vs Public Company The main categories of difference are trading of shares, ownership (types of investors), reporting requirements, access to capital, and valuation considerations.

What does being publicly private mean?

Companies that go private are no longer listed or traded on a public stock exchange. Going private also means that they no longer need to report financial information to the Security and Exchange Commission (SEC) or follow many of its rules. Advertisement.


1 Answers

Basically, yes, it's to prevent people from screwing with things.

Encapsulation (information hiding) is the term you're looking for.

By only publishing the bare minimum of information to the outside world, you're free to change the internals as much as you want.

For example, let's say you implement your phone book as an array of entries and don't hide that fact.

Someone then comes along and writes code which searches or manipulates your array without going through your "normal" interface. That means that, when you want to start using a linked list or some other more efficient data structure, their code will break, because it's used that information.

And that's your fault for publishing that information, not theirs for using it :-)

Classic examples are the setters and getters. You might think that you could just expose the temperature variable itself in a class so that a user could just do:

Location here = new Location();
int currTemp = here.temp;

But, what if you wanted to later have it actually web-scrape information from the Bureau of Meteorology whenever you asked for the temperature. If you'd encapsulated the information in the first place, the caller would just be doing:

int currTemp = here.getTemp();

and you could change the implementation of that method as much as you want. The only thing you have to preserve is the API (function name, arguments, return type and so on).


Interestingly, it's not just in code. Certain large companies will pepper their documentation with phrases like:

This technical information is for instructional purposes only and may change in future releases.

That allows them to deliver what the customer wants (the extra information) but doesn't lock them in to supporting it for all eternity.

like image 196
paxdiablo Avatar answered Sep 28 '22 12:09

paxdiablo