In Java, access modifiers are used to control the accessibility of classes, methods, and variables. There are three access modifiers in Java: public, private, and protected.
The public access modifier makes a class, method, or variable accessible from anywhere in the program, regardless of the package that it belongs to. This means that any other class in any package can access the public class, method, or variable.
The private access modifier makes a class, method, or variable accessible only within the same class. This means that no other class can access the private class, method, or variable.
The protected access modifier makes a class, method, or variable accessible within the same package and to subclasses in other packages. This means that any class in the same package as the protected class, method, or variable can access it, as well as any subclass of the class that contains the protected class, method, or variable.
Here’s an example Java program that demonstrates the difference between public, private, and protected access modifiers:
package com.example.package1;
public class MyClass1 {
public int publicVariable = 0;
private int privateVariable = 0;
protected int protectedVariable = 0;
public void publicMethod() {
System.out.println("This is a public method.");
}
private void privateMethod() {
System.out.println("This is a private method.");
}
protected void protectedMethod() {
System.out.println("This is a protected method.");
}
}
package com.example.package2;
import com.example.package1.MyClass1;
public class MyClass2 extends MyClass1 {
public void myMethod() {
publicVariable = 1;
// privateVariable = 1;
// Error: privateVariable has private access in MyClass1
protectedVariable = 1;
publicMethod();
// privateMethod();
// Error: privateMethod() has private access in MyClass1
protectedMethod();
}
}
public class Main {
public static void main(String[] args) {
MyClass1 myObject1 = new MyClass1();
myObject1.publicVariable = 1;
// myObject1.privateVariable = 1;
// Error: privateVariable has private access in MyClass1
// myObject1.protectedVariable = 1;
// Error: protectedVariable has protected access in MyClass1
myObject1.publicMethod();
// myObject1.privateMethod();
// Error: privateMethod() has private access in MyClass1
// myObject1.protectedMethod();
// Error: protectedMethod() has protected access in MyClass1
MyClass2 myObject2 = new MyClass2();
myObject2.publicVariable = 1;
// myObject2.privateVariable = 1;
// Error: privateVariable has private access in MyClass1
myObject2.protectedVariable = 1;
myObject2.publicMethod();
// myObject2.privateMethod();
// Error: privateMethod() has private access in MyClass1
myObject2.protectedMethod();
}
}
In this program, we define two classes (MyClass1 and MyClass2) and a Main class. The MyClass1 class has three instance variables (publicVariable, privateVariable, and protectedVariable) with public, private, and protected access modifiers, respectively. It also has three methods (publicMethod, privateMethod, and protectedMethod) with public, private, and protected access modifiers, respectively.
The MyClass2 class extends MyClass1 and defines a method (‘myMethod‘)