Which access specifier does not allow class members directly outside class?

The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it.

There are four types of Java access modifiers:

  1. Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
  2. Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
  3. Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
  4. Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

There are many non-access modifiers, such as static, abstract, synchronized, native, volatile, transient, etc. Here, we are going to learn the access modifiers only.


Understanding Java Access Modifiers

Let's understand the access modifiers in Java by a simple table.

Access Modifierwithin classwithin packageoutside package by subclass onlyoutside packagePrivateYNNNDefaultYYNNProtectedYYYNPublicYYYY

1) Private

The private access modifier is accessible only within the class.

Simple example of private access modifier

In this example, we have created two classes A and Simple. A class contains private data member and private method. We are accessing these private members from outside the class, so there is a compile-time error.

Role of Private Constructor

If you make any class constructor private, you cannot create the instance of that class from outside the class. For example:

Note: A class cannot be private or protected except nested class.


2) Default

If you don't use any modifier, it is treated as default by default. The default modifier is accessible only within package. It cannot be accessed from outside the package. It provides more accessibility than private. But, it is more restrictive than protected, and public.

Example of default access modifier

In this example, we have created two packages pack and mypack. We are accessing the A class from outside its package, since A class is not public, so it cannot be accessed from outside the package.

In the above example, the scope of class A and its method msg() is default so it cannot be accessed from outside the package.


3) Protected

The protected access modifier is accessible within package and outside the package but through inheritance only.

The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.

It provides more accessibility than the default modifer.

Example of protected access modifier

In this example, we have created the two packages pack and mypack. The A class of pack package is public, so can be accessed from outside the package. But msg method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.


4) Public

The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.

Example of public access modifier


Java Access Modifiers with Method Overriding

If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive.

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.

}; 

Example of Access Specifiers in C++

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 Specifier

This 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 Specifiers

The 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.

Which access specifier does not allow class members to be accessed from outside the class but allows them to be accessed by derived classes protected?

The protected access specifier allows the class the member belongs to, friends, and derived classes to access the member. However, protected members are not accessible from outside the class.

Which one of the class members are not allowed to use outside the class?

2. 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 of the following access specifier doesn't allow data to be accessed outside the class except if they are inherited?

Explanation: Protected access is used to make the members private. But those members can be inherited.

Which class members are accessed from outside the class?

Public members (generally methods declared in a class) are accessible from outside the class. The object of the same class is required to invoke a public method.