Using private and public is called Encapsulation. This is a simple understanding that a software package (class or module) needs internal and external.
External (public) is your contract with the rest of the world. You should try to keep it simple, consistent, obvious, reliable and, very importantly, stable.
If you are interested in a good software design, this is just a rule: make all data confidential and make methods available only if necessary.
The principle of data hiding is that the sum of all the fields in the class determines the state of the objects. For a well-written class, each object must be responsible for maintaining the actual state. If part of the state is publicly available, the class will never be able to provide such guarantees.
A small example, suppose that:
class MyDate { public int y, m, d; public void AdvanceDays(int n) { ... }
You cannot prevent a class user from ignoring AdvanceDays () and simply do:
date.d = date.d + 1; // next day
But if you make y, m, d private and test all your MyDate methods, you can guarantee that the system will have valid dates.
Henk holterman
source share