I don't understand the difference between protected and private members or methods, as I assumed both will hide the member or the function to access from outside the class.
What is the difference between the protected and the private keywords?
public : accessible from everywhere. protected : accessible by the classes of the same package and the subclasses residing in any package. default (no modifier specified): accessible by the classes of the same package. private : accessible within the same class only.
prot is inherited as protected. pub and getPVT() are inherited as public. pvt is inaccessible since it is private in Base .
Private : Access is possible only from inside the class (other methods). Protected : Access is possible only for inheriting classes.
Protected means that a class and its subclasses have access to the variable, but not any other classes, they need to use a getter/setter to do anything with the variable. A private means that only that class has direct access to the variable, everything else needs a method/function to access or change that data.
private
- only available to be accessed within the class that defines them.
protected
- accessible in the class that defines them and in other classes which inherit from that class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With