When and why should I use public
, private
, and protected
functions and variables inside a class? What is the difference between them?<
Visibility Scopes with Abstract Examples :: Makes easy Understanding
This visibility of a property or method is defined by pre-fixing declaration of one of three keyword (Public, protected and private)
Public : If a property or method is defined as public, it means it can be both access and manipulated by anything that can refer to object.
Protected : when a property or method visibility is set to protected members can only be access within the class itself and by inherited & inheriting classes. (Inherited:- a class can have all the properties and methods of another class).
Private : When a property or method visibility is set to private, only the class that has the private members can access those methods and properties(Internally within the class), despite of whatever class relation there maybe.