Which access specifier does not allow class members to be accessed from outside the class?
Data hiding is an important concept of Object-Oriented Programming, implemented with these Access modifiers' help. It is also known as Access Specifier. Access Specifiers in a class decide the accessibility of the class members, like variables or methods in other classes. That is, it will decide whether the members or methods will get directly accessed by the blocks present outside the class or not, depending on the type of Access Specifier. In a
program, we need to create methods or variables that can be accessed by the object of the same class or accessible in the entire program. And Access Modifiers help us to specify that. There are three types of access modifiers in C++: To manipulate and fetch the data, a public specifier is used, and to protect the data from outside members, a private specifier is used so that the crucial or sensitive data
cannot be tampered with or leaked outside of its block. The syntax to use these specifiers is very simple. We need to mention the type of specifier with a colon just before the class's variable or methods. class ClassName {
private:
// Declare private members/methods here.
public:
// Declare public members/methods here.
protected:
// Declare protected members/methods here.
};
Suppose
there is a company with different departments to do specific tasks. Since the HR department works in different areas, they also manage the company's finance. They credit the salary to the employee account every month and handle all the money-related issues. And the salary is one such thing, which everybody should not have access to. It should be accessible by the HR department or the employee-manager, who can update their salaries after the appraisal. So, if there is a class named Employee
where there are different variables, the information related to them like their Employee Id, Employee name, salary, Date of Joining, performance rating, etc. And we have to design the system so that every employee can find an employee's name, date of joining, etc., using the employee id, but salary can not be accessible to all. These access specifiers or access modifiers come into use. We can make the salary variable private and the rest all public, and for updating the salary or getting the
salary, we can make proper methods for that. Example - There are three variables in an Employee Class. One is Employee name, the second is Employee Id, and the third is Employee Salary. class Employee { private: int employeeSalary; public: int employeeId; string employeeName; string getEmployeeName() { return employeeName; } protected: void setEmployeeSalary(int n) { employeeSalary = n; return; } }; Here, employeeSalary is a private access specifier, but employeeId and employeeName are public access specifiers. And the setEmployeeSalary method/function is a protected specifier. Types of Access Specifiers & How do they work in C++?1. Public Access SpecifierThis keyword is used to declare the functions and variables public, and any part of the entire program can access it. The members and member methods declared public can be accessed by other classes and functions. The public members of a class can be accessed from anywhere in the program using the (.) with the object of that class. Example - In the above example,employeeId and employeeName are public access specifiers. 2. Private Access SpecifiersThe private keyword is used to create private variables or private functions. The private members can only be accessed from within the class. Only the member functions or the friend functions are allowed to access the private data of a class or the methods of a class. Note -
Example - In the above example,employeeSalary is a private access specifier. 3. Protected Access SpecifiersThe protected keyword is used to create protected variables or protected functions. The protected members can be accessed within and from the derived/child class.
For more details about derived class or inheritance, you can follow this blog. Inheritance in cpp Protected access specifier is similar to the private modifier. It cannot be accessed outside of its class except the derived class or subclass of that class. However, it can be accessed by the friend function of that class, similar to a private specifier. Example - In the above example,setEmployeeSalary method/function is a protected specifier. Conclusion
Which access specifier does not allow class members directly outside class?Private: The class members declared as private can be accessed only by the member functions inside the class. They are not allowed to be accessed directly by any object or function outside the class.
Which type of access modifier members are not accessible outside the class?Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class. Default: The access level of a default modifier is only within the package.
Which modifier is used to specify that a method Cannot be used outside a class?Any variable, method, or class declared to use the default access modifier cannot be accessed by any other class outside of the package from which it was declared.
Which access modifier allows accessibility from any outside class?Public Access Modifier
A class or a method or a data field specified as 'public' is accessible from any class or package in the Java program. The public entity is accessible within the package as well as outside the package.
|