What is the difference between private and protected? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

What is the difference between private and protected?

2nd Nov 2016, 1:07 PM
Shruti Singh
Shruti Singh - avatar
6 Answers
+ 7
Public - visible to all classes. Private - visible only to the class to which they belong. Protected - visible only to the class to which they belong, and any subclasses. They is referring things like variables, methods/functions within a class.
2nd Nov 2016, 1:44 PM
Malvern Panashe Mhepo
Malvern Panashe Mhepo - avatar
+ 4
Private makes it so only that class can access these functions. Protected allows inherited/derived classes which extend from the main class access the main class' (which they extend) methods/variables.
2nd Nov 2016, 1:45 PM
nedas
nedas - avatar
+ 3
Private and Protected are access specifiers. Only the Base Class can have Private access to its data and methods while Protected access allows the Base Class and the class derived from the Base Class(also referred to as Derived Class/Inherited Class) to access.
10th Nov 2016, 10:52 AM
Orca
Orca - avatar
+ 1
Private : means accessible to only the base class(parent class),i.e. all the data members and data functions are accessible to the class that defines them. Protected : means accessibility to the derived class(child class),i. e. all the data members and data functions are accessible in the class that defines them and in other classes which inherit from that class.
4th Nov 2016, 5:04 AM
Sarthak Sareen
Sarthak Sareen - avatar
0
private: the variable or function declared under private are accessed by the member function of that class only. not to the subclasses which inherit the class protected: all the variables and function declared as protected can be accessed by the member function of that class and its subclasses. if the subclass inherit it as public or protected protected -> is same as protected if its privately extended protected -> private
2nd Nov 2016, 3:44 PM
lokesh
lokesh - avatar
0
protected only work when you use the concept of inheritence
8th Dec 2016, 4:36 PM
Tahseen