What is Access Modifier ?
Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components.
Let's Consider a real-life example:
The headquarters wing, having 15 core members has come in possession of sensitive confidential information regarding national security. Now we can correlate these core members to data members or member functions of a class which in turn can be correlated to the headquarter wing. These 15 members can directly access confidential information from their wing (the class), but anyone apart from these 15 members can’t access this information directly i.e. outside functions other than those prevalent in the class itself can’t access information that is not entitled to them, without having either assigned privileges or access to one of these 15 members who is allowed direct access to the confidential information. This is what data hiding is in practice.
Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.
Types of Access Modifier?
Public
Private
Default
Protected
Let's discuss them one by one....
• Public
All the class members(variables) declared under the public specifier will be available to everyone. The data members and member functions declared as public can be accessed by other classes and functions too.
Also, all the data members are accessible from the same and different packages.
The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.
• Private
All the class members declared under the private specifier will be available only to the class itself. The data members and member functions declared as private cannot be accessed by other classes and functions.
Also, all the data members are inaccessible from the different packages.
The private members of a class cannot be accessed from anywhere in the program. These data members can only be accessed using the getter and setter functions(defined under public scope).
• Default
All the class members declared under the default specifier will be available only to the same package. The data members and member functions declared as default cannot be accessed by other packages.
The default members of a class can be accessed from anywhere in the program from the same package but not from the other package.
The data member without any access specifier defined explicitly is set to default.
• Protected
All the class members declared under the protected specifier will be available to everyone except to the non-sub classess of different packages.
The protected members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.
Why do we use access specifiers in our programs?
Access modifiers are mainly used for encapsulation. It can help us to control what part of a program can access the members of a class. So that misuse of data can be prevented.
Definition of Jargon for beginner
- Data Member: A data member may be of any type, including classes already defined, pointers to objects of any type, or even references to objects of any type
- Member function: Member functions are operators and functions that are declared as members of a class
- Encapsulation: Encapsulation is an object-oriented procedure of combining the data members and member functions of the class inside the user-defined class.
.
.
.
Thanks for reading, I hope you liked this blog. If you find it beneficial then don’t forget to like and share among your peers and friends :)
Top comments (4)
Nice 😃
Thanks Rudrakshi!!
well explained. very informative🙌
Thanks Jyoti!!