Access Modifiers in Java - Private, Default, Protected, Public Modifiers

Access Modifiers in Java are used to restricts access of a class, constructor, data member, and method in another class. There are four types of access modifiers available in Java:

1. Private Access Modifier.
2.Default Access Modifier.
3. Protected Access Modifier.
4. Public Access Modifier.

Access Modifiers Table: Let's understand Java access modifiers with the help of the table.
Access Modifiers in java


1. Private Access Modifier:

The private access modifier is only accessible within the same class in which they are declared. The data member and method declared as private cannot be accessed from outside the class. If you try to access the private data member and method from outside the class then the compiler will give you a compile-time error.



Example of Private Access Modifier:

class PrivateModifier{
	private int num = 10;
	private void display(){
		System.out.println("Value of num is: " +num);
	}
}
class AccessModifiers{
	public static void main(String args[]){
		PrivateModifiers obj = new PrivateModifiers();
		System.out.println(obj.num);
		obj.display();
	}
}

Output:

Compile time error:
error: num has private access in PrivateModifier(class)
          System.out.println(obj.num);
error: display() has private access in PrivateModifier(class)
          obj.display();



2. Default Access Modifier:

The class, data member, and method that are not declared using any modifier, it treated as default access modifier by default. The default access modifier will be accessible within the same package. If you try to access the default modifier from outside the package then the compiler will give you a compile-time error.


Example of Default Access Modifier:

In this example, we will create two packages pack1 and pack2 and the classes inside the packages are default access modifier and we accessing the class A from outside the package. Since class A is not public therefore it cannot be accessible from outside the package.

//save by A.java
package pack1;
class A{
	void display(){
		System.out.println("Inside class A method. ");
	}
}


//save by B.java
package pack2;
import pack1.*;
class B{
	public static void main(String args[]){
		A obj = new A();
		obj.display(); //compile-time error.
	}
}

Output:

compile time error.


3. Protected Access Modifier:

The data member and method declared as protected can be only accessible within the same package and outside the package through inheritance only. It cannot be applied to classes.



Example of Protected Access Modifier:

In this example, we will create two packages pack1 and pack2 and class A in the package pack1 is made public so it can be accessed from outside the package but the method display() declared inside this package is protected, so it can be accessed from outside the class through inheritance only.

//save by A.java
package pack1;
class A{
	protected void display(){
		System.out.println("Inside class A method. ");
	}
}


//save by B.java
package pack2;
import pack1.*;
class B extens A{
	public static void main(String args[]){
		B obj = new B();
		obj.display(); 
	}
}

Output:

inside class A method.


4. Public Access Modifier: 

The members, methods, and classes declared as public can be accessed from anywhere. This modifier can be accessed from within the class, outside the class, within the package, and outside the package. 


Example of Public Access Modifier:

In this example, we will create two packages pack1 and pack2 and class A and method display() inside the package pack1 is public, so it can be accessed from anywhere.

//save by A.java
package pack1;
public class A{
	public void display(){
		System.out.println("Inside class A method. ");
	}
}


//save by B.java
package pack2;
import pack1.*;
class B{
	public static void main(String args[]){
		A obj = new A();
		obj.display(); //compile-time error.
	}
}

Output:

inside class A method.



You may also like these posts:

1. Final keyword in java with example.
Access Modifiers in Java - Private, Default, Protected, Public Modifiers Access Modifiers in Java - Private, Default, Protected, Public Modifiers Reviewed by Prashant Srivastava on December 26, 2019 Rating: 5

No comments:

Powered by Blogger.