+ 9
What is the difference between private and protected?
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.
+ 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.
+ 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.
+ 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.
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
0
protected only work when you use the concept of inheritence