Java Programming
About Lesson

What is an Access Modifier?
Access Modifiers are used to control the accessibility of data members or member methods of a class to its objects and rest of the program.

Access Specifier Vs Access Modifier
There are no Access Specifiers in java but only Access Modifiers. If we have not mentioned any Access modifier then the member’s access level is considered as default access.

Is there any default modifier?
No, there is no default modifier but default access level. If no access modifier is specified, then the program element may have default access level (also known as package-private). There is no keyword called ‘default’ declare default accessibility. Here you should not confuse with a keyword ‘default’ which we use in switch case block.

How many types of Access Modifiers?
1. Access Modifiers: they are used to modify the default accessibility of any program element (i.e. class, method, data member, interface etc)
2. Non-Access Modifiers: these are used for not to change the accessibility but some specific functionality of program element or behaviour of program element with JVM.

Both Access and Non-access Modifiers can be applied to program elements like class(outer or inner), methods, constructors, data members, interfaces. But some of access and Non-access modifiers cannot be used with certain program elements.

Since we have studied only class (that too top-level class), object and constructor, we are only discussing about meaning of access and non-access modifiers for class, members and constructors only in this chapter. We will discuss meaning of access and non-access modifiers regarding other program elements later in this course.

The three access modifiers of java are:
Private – Basic meaning of this modifier is accessible within the class among the members.
Public – Accessible anywhere (i.e. outside the package or even other project also)
Protected – Accessible to subclasses and package only

 

Table showing visibility of program elements defined with Modifiers:

Modifier

Class

Subclass

Package

World

Private

Yes

 

 

 

Protected

Yes

Yes

Yes

 

Public

Yes

Yes

Yes

Yes

<Not mentioned>

Yes

 

Yes

 

 

Access Modifiers in the order of most restricted to more accessible:
Private – Most Restricted
Protected – Less restricted than Private
<not mentioned>default Access Level – Less restricted than protected
Public – Most Accessible

Access Modifiers and their meaning with respect to a Top-level or Outer class:
Public: can instantiate the class outside the package too.
<default>: can instantiate the class within the package only.
The modifiers private and protected cannot be used for top-level or outer classes.

Access Modifiers and their meaning with respect to members(data/methods):
Public: can access it outside the package also with its class object.
<default>: can access it within the package only with its class object.
Private: can access it only within the class among its class members.
Protected: can be accessible to immediate sub class

Access Modifiers and their meaning with respect to a Constructor:
Since constructor is a special method of class which has the same name of its class and used to initialize its class members, Constructor cannot be directly called or inherited to subclasses. Since there is no question of inheritance, there is no question of overriding too.

We can use any one of the modifiers in the header of a constructor since it is nothing but a class member.
Public: we can access it outside the package also with its class object.
<default>: we can access it within the package only with its class object.
Private: cannot be visible to its sub class constructors.
Protected: can be visible to sub class constructors.

 

You cannot copy content of this page