In Java, an access modifier is a keyword that specifies the visibility or accessibility of a class, method, or field within the program.
Java provides four types of access modifiers:
Syntax
public class MyClass {
private int myPrivateField; // myPrivateField accessible within class only
private void myPrivateMethod() {
// Code here
}
}
Example:
class MyClass {
// Private field accessible only within the same class
private int x;
// Private method accessible only within the same class
private void fun() {
System.out.println("Executing private method!");
}
// Public method to access the private field indirectly
public void show(int value) {
// Accessing the private field within the same class
x= value;
System.out.println("Assigning private field to: " + x);
}
// Public method to access the private method indirectly
public void calling() {
// Accessing the private method within the same class
fun();
}
public static void main(String[] args) {
// Creating an instance of MyClass
MyClass Obj = new MyClass();
// Accessing the public methods to interact with private members
Obj .show(42);
Obj .calling();
}
}
Output:
Assigning private field to: 42
Executing private method!
Syntax
class PackagePrivateClass {
int packagePrivateField; //
void packagePrivateMethod() {
// Code here
}
}
Example
package LearnCoding;
class A {
// Package-Private method
void show() {
System.out.println("Package-Private method");
}
}
class B {
public static void main(String[] args) {
// Creating an instance of DefaultAccessExample
A obj = new A();
obj.show();
}
}
Explanation
show() method is default method in above example.
show() method can accessible by other class also but within LearnCoding package only.
Declaring protected Variables, Methods and Constructors
public class FirstClass {
protected String name;
protected FirstClass(String name) {
this.name = name;
}
protected String getName() {
return name;
}
}
Accessing protected Fields, Methods, and Constructors
1. From the Same Package
creating a new GenericClass declared in the same package as FirstClass
public class GenericClass {
public static void main(String[] args) {
FirstClass first = new FirstClass("random name");
System.out.println("FirstClass name is " + first.getName());
first.name = "new name";
}
}
2. From a Different Package
now try to interact with these fields from a class declared in a different package from FirstClass
public class SecondGenericClass {
public static void main(String[] args) {
FirstClass first = new FirstClass("random name");
System.out.println("FirstClass name is "+ first.getName());
first.name = "new name";
}
}
we getting compilation errors:
The constructor FirstClass(String) is not visible
The method getName() from the type FirstClass is not visible
The field FirstClass.name is not visible
3. From a Sub-Class
public class SecondClass extends FirstClass {
public SecondClass(String name) {
super(name);
System.out.println("SecondClass name is " + this.getName());
this.name = "new name";
}
}
Explanation:
we can access all the protected fields, methods, and constructors. This is because SecondClass is a sub-class of FirstClass.
Syntax
public class PublicClass {
public int publicField;
public void publicMethod() {
// Code here
}
}
Example
// Public class
public class PublicAccessExample {
// Public variable
public int publicVariable = 42;
// Public method
public void publicMethod() {
System.out.println("Public method");
}
}
public class AnotherClass {
public static void main(String[] args) {
// Creating an instance of PublicAccessExample
PublicAccessExample example = new PublicAccessExample();
// Accessing public variable and method from AnotherClass
System.out.println("Public variable: " + example.publicVariable);
example.publicMethod();
}
}