dsa

Java Access Modifiers

In this tutorial, we will learn about the Java Access Modifier, its types, and how to use them with the help of examples.

Access Modifier:- As the name suggests access modifiers in Java helps to restrict the scope of a class, constructor , variable , method or data member.Java provides both Access Specifier and Access Modifiers for creating access to your Java code for other classes. Here modifier is also used to do the same task but there are limitations.

There are four types of access modifiers available in java:

Modifier Description
Default declarations are visible only within the package (package private)
Private declarations are visible within the class only
Protected declarations are visible within the package or all subclasses
Public declarations are visible everywhere

1.Default Access Modifier

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.

For Example:

//Java program to illustrate default modifier 
package p1; 

//Class cod having Default access modifier 
class cod
{ 
	void display() 
	{ 
		System.out.println("Hello World!"); 
	} 
} 

    
    
//Java program to illustrate error while 
//using class from different package with 
//default modifier 
package p2; 
import p1.*; 

//This class is having default access modifier 
class codenew
{ 
	public static void main(String args[]) 
	{ 
		//accessing class cod from package p1 
		cod obj = new cod(); 

		obj.display(); 
	} 
} 
output:
   Compile time error

Here, the cod class has the default access modifier. And the class is visible to all the classes that belong to the defaultPackage package. However, if we try to use the cod class in another class outside of defaultPackage, we will get a compilation error.

2.Private Access Modifier

The access level of a private modifier is only within the class. It cannot be accessed from outside the class.

ForExample:

//Java program to illustrate error while 
//using class from different package with 
//private modifier 
package p1; 

class A 
{ 
private void display() 
	{ 
		System.out.println("Codemistic"); 
	} 
} 

class B 
{ 
public static void main(String args[]) 
	{ 
		A obj = new A(); 
		//trying to access private method of another class 
		obj.display(); 
	} 
} 


Output:

error: display() has private access in A
        obj.display();

In this example, we have create two classes A and B within same package p1. We will declare a method in class A as private and try to access this method from class B and see the result.

3.Protected Access Modifier

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.

ForExample:

//Java program to illustrate 
//protected modifier 
package p1; 

//Class A 
public class A 
{ 
protected void display() 
	{ 
		System.out.println("Codemistic"); 
	} 
} 


//Java program to illustrate 
//protected modifier 
package p2; 
import p1.*; //importing all classes in package p1 

//Class B is subclass of A 
class B extends A 
{ 
public static void main(String args[]) 
{ 
	B obj = new B(); 
	obj.display(); 
} 
	
} 


Output:
Codemistic

In this example, we have created two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method display in class A is protected and class B is inherited from class A and this protected method is then accessed by creating an object of class B.

4.Public Access Modifier

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.

ForExample:

//Java program to illustrate 
//public modifier 
package p1; 
public class A 
{ 
public void display() 
	{ 
		System.out.println("Codemistic"); 
	} 
} 


package p2; 
import p1.*; 
class B 
{ 
	public static void main(String args[]) 
	{ 
		A obj = new A; 
		obj.display(); 
	} 
} 

Output:
Codemistic

In this example, we have created two packages p1 and p2. Class A in p1 is made public, from every where in the program. There is no restriction on the scope of a public data members.

Important Points: