Asked • 03/26/19

Private vs Protected - Visibility Good-Practice Concern?

I've been searching and I know the theoretic difference. - **public** - Any class/function may access the method/property. - **protected** - Only this class and any subclasses may access the method/property. - **private** - Only this class may access the method/property. It won't even be inherited.That's all fine and well, the question is, what's the **practical** difference between them? When would you use `private` and when would you use `protected`? Is there a standard or acceptable good practice over this one?Up until now, to retain the concept of inheritance and polymorphism, I use `public` for anything that should be accessed from the outside (like constructors and main class functionality), and `protected` for internal methods (logic, helper methods etc). Am I on the right track?(Note that this question is for me, but also for future reference as I haven't seen a question like this one SO).

1 Expert Answer

By:

Larry C. answered • 03/27/19

Tutor
4.9 (294)

Computer Science and Mathematics professional

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.