Java Access Modifiers: Control and Security Explained
Table of Content:
Access modifiers in Java helps to restrict the scope of a class, constructor , variable , method or data member.
There are two types of modifiers in java: access modifiers and non-access modifiers.
There are three access modifiers. Not including default access modifier . Default is an access control which will be set when one does not specify any access modifier.
Access Control:
- Public
- Private
- Protected
- Default (No keyword required)
There are many non-access modifiers such as static, abstract, transient, synchronized, native, volatile, etc. Here, we will learn access modifiers.
Default
When no access modifier is specified for a class , method or data member – It is said to be having the default access modifier by default. If you don't use any modifier, it is treated as default by default. The default modifier is accessible only within package i.e The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier areaccessible to the classes which are defined in the same package.Example of default access modifier
Program:
In this example, we have created two packages pack1
and mypackage
.
We are accessing the ClassA
class from outside its package,
since ClassA class is not public, so it cannot be accessed from outside the package.
//save by ClassA.java package pack1; //Class ClassA is having Default access modifier class ClassA{ void msg(){ System.out.println("Hello"); } }
//save by ClassB.java package mypackage; import pack1.*; //Class ClassB is having Default access modifier class ClassB{ public static void main(String args[]){ ClassA obj = new ClassA();//Compile Time Error obj.msg();//Compile Time Error } }
In the above example, the scope of class ClassA and its method msg() is default so it cannot be accessed from outside the package.
Output:
HelloPrivate
The private access modifier is specified using the keyword private.
- The methods or data members declared as private are accessible only within the class in which they are declared.
- Any other class of the same package will not be able to access these members.
- Classes or interface can not be declared as private.
Program:
In this example, we will create two classes ClassA and ClassB within same package p1. We will declare a method in class ClassA as private and try to access this method from class ClassB and see the result.
//Java program to illustrate error while //using class from different package with //private modifier package p1; class ClassA { private void display() { System.out.println("Hello in private"); } } class ClassB { public static void main(String args[]) { ClassA obj = new ClassA(); //trying to access private method of another class obj.display(); } }
Output:
error: display() has private access in A obj.display();Program:
In this example, we have created two classes ClassA and Test. ClassA class contains private data member and private method. We are accessing these private members from outside the class, so there is compile time error.
class ClassA{ private int data=70; private void msg(){ System.out.println("Hello java"); } } public class Test{ public static void main(String args[]){ ClassA obj1=new ClassA(); System.out.println(obj1.data);//Compile Time Error obj1.msg();//Compile Time Error } }
Private Constructor
Program:
If you make any class constructor private, you cannot create the instance of that class from outside the class. For example:
class ClassA{ private ClassA(){}//private constructor void msg(){ System.out.println("Hello java"); } } public class Test{ public static void main(String args[]){ ClassA obj1=new ClassA();//Compile Time Error } }
Point to be Noted: A class cannot be private or protected except nested class.
protected
The protected access modifier is specified using the keyword protected.
- The methods or data members declared as protected are accessible within same package or sub classes in the different package.
It 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.
Program:
In this example, we will create two packages pack1
and pack2
.
Class ClassA
in pack1
is made public,
to access it in pack2. The method display in class ClassA is protected and class ClassB is inherited
from class ClassA and this protected method is then accessed by creating an object of class ClassB.
//Java program to illustrate //protected modifier package pack1; //Class ClassA public class ClassA { protected void display() { System.out.println("Hello"); } } //Java program to illustrate //protected modifier package pack2; import pack1.*; //importing all classes in package pack1 //Class ClassB is subclass of ClassA class ClassB extends ClassA { public static void main(String args[]) { ClassB obj = new ClassB(); obj.display(); } }
Output:
Helloprotected
The public access modifier is specified using the keyword public
.
- The public access modifier has the widest scope among all other access modifiers.
- Classes, methods or data members which are declared as the public are accessible from every where in the program. There is no restriction on the scope of a public data members.
Program:
//save by ClassA.java package pack1; public class ClassA{ public void msg(){ System.out.println("Hello"); } }
//save by ClassB.java package mypackage; import pack1.*; class ClassB{ public static void main(String args[]){ ClassA obj1 = new ClassA(); obj1.msg(); } }
Output:
HelloJava Access Modifiers Table for better Understanding
Let's understand the access modifiers by a simple table.
Access Modifier | 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 |
Java Access Modifiers Table for better Understanding
Visibility | Public Access Modifier | Private Access Modifier | Protected Access Modifier | Default Access Modifier |
Within Same Class | Yes | Yes | Yes | Yes |
From Any Class in Same Package | Yes | No | Yes | Yes |
From Any Sub Class in Same Package | Yes | No | Yes | Yes |
From Any Sub Class from Different Package | Yes | No | Yes(Only By Inheritance) | No |
From Any Non Sub Class in Different Package | Yes | No | No | No |
Access Control and Inheritance
The following rules for inherited methods are enforced ?
-
Methods declared public in a superclass also must be public in all subclasses.
-
Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.
-
Methods declared private are not inherited at all, so there is no rule for them.
access modifiers with method overriding
Program:
class ClassA{ protected void display(){ System.out.println("Hello java"); } } public class Simple extends ClassA{ void display(){ System.out.println("Hello java");}//Compile Time Error public static void main(String args[]){ Simple obj=new Simple(); obj.display(); } }
The default modifier is more restrictive than protected. That is why there is compile time error.