Access modifiers in Java control the visibility and accessibility of classes, methods, and variables. They define who can access a particular entity in the program.
Types of Access Modifiers
There are four types of access modifiers provided in java,
- public
- protected
- private
- default (no modifier)
1. public
Access Modifier
- It is accessible from anywhere (inside and outside the class, package, and subclass).
- It has no restrictions on visibility.
// save into file MyClass.java
package mypackage;
public class MyClass {
public void showMessage() {
System.out.println("Public method can be accessed anywhere!");
}
}
// save into another file Test.java
package anotherpackage;
import mypackage.MyClass;
public class Test {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.showMessage();
}
}
Output
Public method can be accessed anywhere!
2. private
Access Modifier
- It is accessible only within the same class.
- It is not accessible in subclasses or other classes.
- It is used for data hiding.
Example
class Person {
private String name = "Alice";
private void display() {
System.out.println("Name: " + name);
}
public static void main(String[] args) {
Person obj = new Person();
obj.display(); // Accessible inside the same class
}
}
class Test {
public static void main(String[] args) {
Person obj = new Person();
// obj.display(); ERROR: Private method not accessible
}
}
Output (from Person
class's
main
method)
Name: Alice
The display()
method cannot be accessed from outside the Person class.
3. protected
Access Modifier
- It is accessible within the same package and subclasses outside the package.
- It is used for inheritance.
Example
package mypackage;
class Animal {
protected void sound() {
System.out.println("Animals make sounds...");
}
}
package anotherpackage;
import mypackage.Animal;
class Dog extends Animal {
public void bark() {
sound(); // Accessible in subclass
}
}
public class Test {
public static void main(String[] args) {
Dog obj = new Dog();
obj.bark(); // Accessible through subclass
}
}
Output
Animals make sounds...
sound()
cannot be accessed outside of mypackage
unless it is obtained via inheritance.
4. Default Access (No Modifier)
- This is also called package-private access.
- It is accessible only within the same package.
- It is not accessible outside the package (even to subscales).
Example
package mypackage;
class Vehicle {
void display() {
System.out.println("This is a vehicle.");
}
}
package anotherpackage;
import mypackage.Vehicle;
public class Test {
public static void main(String[] args) {
Vehicle obj = new Vehicle();
obj.display(); // ERROR: Cannot access default method outside package
}
}
The compilation error occurs because display()
is not accessible outside of
mypackage
.
Final thaught
Modifier | Scope | Used For |
public |
Anywhere | Maximum accessibility (libraries, APIs, frameworks) |
private |
Within the same class | Data hiding (Encapsulation) |
protected |
Same package + subclasses | Inheritance (OOP concept) |
Default | Same package only | Package-level access control |
Also, Read: Explain the Constructors in Java
Leave Comment