Data Types, Variables and Constants

Access Modifiers

Access Modifiers or Access specifiers in programming are used to implement the accessibility of class, methods, constructors, or other members in the program. Access Modifiers can define how the stored data variables of a class can be accessed and modified. You must have observed keywords used in programs such as public, private, protected. These are nothing but Access Modifiers. Access Specifiers are used to specify how the data is enclosed and used in a program. Let’s learn more about Access Modifiers and their types in this article below.

Definition

Access Modifiers (Access Specifiers) are keywords that are used in OOP (object-oriented programming) in order to specify the accessibility of the methods, classes, constructors, and other members of the class.

Access Modifier is not a concept but rather keywords that are used in programs. They tell the compiler which class or method should have the access to the data or method being defined.

Why do we use it?

Access Modifiers are mainly created for the purpose of Encapsulation i.e. Data Hiding. Encapsulation or Data Hiding is a method in object-oriented programming where the values of the variables or the state of the data object are kept hidden or enclosed inside a class. These enclosed values prevent client’s direct access and restrict them to some extent, so as to secure the data and not expose important details of data maintained by the class or methods. In short, Access Modifiers are used to define who does or who does not have access to certain features in the class and methods of a program.

Example

Let’s understand a real-life application of Access Modifiers. Suppose you own a small company having a human strength of 30 employees and you have hired 2 employees for the payroll process. These 2 employees work on the monthly salary payment process, increment/bonus, appraisal process, allocating and transferring funds to the employees.

Such data is confidential and it needs to be kept with minimum and important people only. For this purpose, we can use the Access Modifiers wherein the data system of the 2 employees are kept private and will be able to access each other’s Payroll process data and can coordinate with each other for any help or issue, keeping other employees unable to access this confidential data for security purposes.

Types of Access Modifiers

There are a number of different Access Modifiers depending on the programming language. In Java, we have 4 Access Specifiers, whereas in C++ we have 3 types of Access Modifiers.

We will discuss the most common and basic ones.

  1. Default

    When we do not specify any access level for a class, method, or data members, then it assigns Default Access Modifier. The data members, methods, or class inside one package can only be accessed within the package. Any other class or method trying to access the data from outside the package or from other packages will not be granted access to the class or method.

Example: We will create 2 packages p1 and p2 and the classes will be having default access modifiers to illustrate this example.

                    

speed = 40 ;

speed += 10 ;  // equivalent to speed = speed + 10

print (“Speed = ”, speed) ;

Output –

Speed = 50

  1. Public

    The classes, methods, or constructors which are declared as Public can be accessed from anywhere. The keyword public is used to define the accessibility level of this type of Access Modifier. They can be accessed from within the same class, from a different class, within the package as well as outside the package.

Example:

                    

//Java program to understand Public Access Modifier

package p1;

//class A is defined as public

public class A {

public void display()

{

System.out.println("Hello World!");

}

}



package p2;

import p1.*;

class B {

public static void main(String args[])

{

//Accessing content of class A from another class B

A obj = new A;

obj.display();

}

}

Output:

Hello World!

  1. Private

    Private methods and data members can only be accessed from within the class I. which they have been declared. They are specified using the private keyword. These modifiers cannot be accessed from outside the class or from any other outside package. Private Access Modifiers cannot be declared for any class or interface.

Example:

                    

//Java program to understand Private Access Modifier

package p1;

class A {

private void display()

{

System.out.println(“Hello World!”);

}

}

class B {

public static void main(String args[])

{

//Accessing private method from class A

A obj = new A();

obj.display();

}

}

Output:

Error: display() has private access in A

obj.display();

  1. Protected

    The methods and data members which are declared as protected are accessible by the classes within the package and the subclasses of those packages. This type of Access Modifier is specified using the protected keyword. Just like private access modifiers, protected access modifiers too cannot be declared for classes.

Example:

                    

//Java program to understand Protected Access Modifier

package p1;

public class X

{

protected void display()

{

System.out.println("How are you?");

}

}



package p2;

import p1.*;

// Class Y is a subclass of X

class Y extends X

{

public static void main(String args[])

{

Y obj = new Y();

obj.display();

}

}

Output:

How are you?

Browse more Topics Under Data Types, Variables and Constants

To summarize the Access Modifiers –

private Least Access Best Encapsulation
protected Some Access Moderate Encapsulation
public Full Access No Encapsulation

 Scope of all Access Modifiers

Class Package Subclass (same package) Subclass (other package) Outside Class
public Yes Yes Yes Yes Yes
protected Yes Yes Yes Yes No
default Yes Yes Yes No No
private Yes No No No No

 

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.