What is public, protected, and private?
In object oriented programming, public, private and protected are types of access modifiers which help in Encapsulation. Here, data and functions can be hid in one class, so that another may not be able to access it. Public, private and protected are three different categorizations that tell the compiler which other classes should have access to the field or method being defined.
- Private - Only the current class will have access to the field or method.
- Protected - Only the current class and subclasses of this class will have access to the field or method. Sometimes the same-package classes may also have access.
- Public - Any class can refer to the field or call the method.