+ 2

I don't understand the usage of access specifiers in coding. Can anyone explain me in detail?

11th Feb 2017, 10:07 AM
3 Answers
+ 8
Functions and variables of a class ... - under "public" can be accessed by all other functions and classes. - under "private" can only be accessed by the class itself. - under "protected" can be accessed by the class itself and classes which inherits from that class.
11th Feb 2017, 11:14 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
public: Can be accessed by any code. protected: Can be accessed only from the class and any derived classes. private: Can be accessed only from the class.
11th Feb 2017, 11:15 AM
Access Specifiers are needed in the context of providing and restricting access to the properties and methods of a class. They are used for data abstraction and data hiding. Usage of access specifiers in coding is very much similar to real life contexts, in coding we have three kind of access specifiers majorly, i.e. 1. public - accessible to every function and variable inside or outside of the class. 2. private - accessible to the function and variable inside the class. Other classes or function can't access the information kept private in the declared class. 3. protected - accessible by the declaring class itself and the classes which are derived from that class. following are some real life examples that can be helpful; 1. Public Transportation viz buses, trains can be used by anyone 2. Your Private vehicles can be used by you and your family(class) members only. 3. Government vehicles can be understood as protected vehicles as they are the property of government insititutes(base class) but the authorized officials can use those vehicles (derived class )to perform their duties. I think this might help you to understand the concept.
16th Feb 2017, 9:45 AM
ashish bansal
ashish bansal - avatar