top of page
Priyanka Pandya

Access Modifiers in Java simplified with Table and Example


 

It is important to understand the scope of the access modifiers in Java so that we know where and when to use this access modifiers to run our code well. In this we will understand the scope of access modifiers with the help of table diagram and some simple to understand examples.


What is Access Modifiers?

Access modifiers in Java are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below:



For a better understanding Access modifiers can be simplified with the below table to have a detailed description of its scope by shortly summarizing it into a below table:

Access Modifiers

Within Class

Within Package

Outside package by subclass only

Outside package

Private

Yes

No

No

No

Default

Yes

Yes

No

No

Protected

Yes

Yes

Yes

No

Public

Yes

Yes

Yes

Yes

Let us understand more about access modifiers with some examples:

Private: The access is limited to class only .

In this example we have 2 classes Demo and Test in same package. Class Demo has private variable. As, we try to access the private variable outside of the class in Test class by creating an object of Demo class in Test class we are getting compile time error as we can not access private data member out side of the class in our case msg variable cannot be accessed outside of Demo class.

Default: If we do not specify any access modifier. It is treated as default by default. The access is limited within the package only.

In this example We have two packages “A” and “B”.

In package A there are two classes: ” Test “ and “Demo”. Demo is defined as default class in which we have declared Default string variable named as msg which can be accessed within the package and class but not outside the package. In B package we have only one class as “Example” .




So, we will understand this with the help of 2 scenarios as below:

Scenario 1: Within package

Here we will use only A package and its class : Demo and Test.

In this example we can successfully use default variable - msg of Demo class in our Test class and print the msg in console because it is within the same package


Scenario 2: Outside Package

Here we will use both “A” and “B” package.



We will import A package which has Default class: Demo and Default variable: msg so that we will try to use it in Example class of “B” package by creating an object of Demo but in this example it is giving compile time error when we try to create an object of Demo class because Demo class is defined as default so it can not be accessed outside the package.

Protected: The access is limited within the package and outside the package but through inheritance only which means subclass. The protected access modifier can be applied on the data member, method and constructor. It can not be applied on the class.

In this example we have 2 packages “A” and “B”. In class Test of Package “A” we will define protected method and try to use that method in Example class of Package “B” through extending Test class in Example class and import package A.

Define Class Test and protected method inside it

Define Class Example and extend Test class to use “printMsg” method:












Public: It is accessible everywhere. It has wider scope.

Again, we have same classes Test and Example of Package “A” and “B” respectively. Now, the access modifier used for “printMsg” method is public so it can be accessed from anywhere by just importing its package.

Define Test class and method with public access modifier.

Define class Example and import the package of Test class and create the object of Test class to use its method.


Hence, we can conclude that that there are four access modifiers: Private, Default, Protected and Public and each of them have different access scope.

32 views0 comments

Recent Posts

See All

Kommentare


bottom of page