What is Access Specifier?
An access specifier defines the scope of a class member. The scope of a class member refers to the portion of the application from which the member can be read and/or written to. It is also known as the accessibility of a member.
It is the privilege of the programmer to decide the use of an access specifier to implement information hiding. You use various types of access specifiers to specify the extent of the visibility of class members.
The basic types of access specifiers are as follows:
- Public Access Specifier
- Private Access Specifier
- Protected Access Specifier
1. Public Access Specifier
The public access specifier allows a class to expose its member variables and member methods to other classes. Any member that is declared public can be accessed from outside the class across applications.
2. Private Access Specifier
The private access specifier allows a class to hide its member variables and member methods from other classes. Therefore, the private members of a class are not visible outside a class. They are visible only to the methods of the same class. Therefore, the data is hidden and cannot be altered by any method other than the member methods of the class.
3. Protected Access Specifier
The protected access specifier allows a class to hide its member variables and member functions from other classes, except those classes that inherit their features from the class. The protected access specifier becomes important while implementing inheritance.
Comments: