Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between protected and private? [duplicate]

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?

like image 266
danijar Avatar asked Oct 08 '12 14:10

danijar


People also ask

What is the difference between protected and private and public?

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.

What is the difference between public/private and protected derivation?

prot is inherited as protected. pub and getPVT() are inherited as public. pvt is inaccessible since it is private in Base .

Which is more secure private or protected?

Private : Access is possible only from inside the class (other methods). Protected : Access is possible only for inheriting classes.

What is the difference between private and protected in OOP?

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.


1 Answers

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.

like image 153
dsgriffin Avatar answered Oct 14 '22 18:10

dsgriffin