---
title: "Explain Access Modifiers in Java"  
description: "Access modifiers in Java control the visibility and accessibility of classes, methods, and variables. They define who can access a particular entity i"  
author: "Ashutosh Patel"  
published: 2025-03-21  
updated: 2025-03-21  
canonical: https://www.mindstick.com/articles/338838/explain-access-modifiers-in-java  
category: "java"  
tags: ["java", "access modifiers", "methods", "java method"]  
reading_time: 3 minutes  

---

# Explain Access Modifiers in Java

[Access modifiers](https://www.mindstick.com/interview/33965/what-are-the-access-modifiers-and-their-scopes-in-java) in Java [control the visibility](https://www.mindstick.com/forum/158926/what-are-the-access-modifiers-in-c-sharp-and-how-do-they-control-the-visibility-of-class-members) and [accessibility](https://answers.mindstick.com/qa/82443/describe-accessibility-modifiers-in-c-sharp) 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](https://www.mindstick.com/forum/33638/protected-access-modifier-c-sharp)**

- It is accessible from anywhere (inside and [outside the class](https://www.mindstick.com/interview/2468/can-you-access-the-private-method-from-outside-the-class), package, and subclass).
- It has no [restrictions](https://www.mindstick.com/news/3098/us-considering-new-restrictions-on-ai-chip-exports-to-china) on visibility.

```java
// 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

```plaintext
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

```java
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)**

```plaintext
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

```java
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

```plaintext
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

```java
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](https://www.mindstick.com/forum/159699/what-is-a-compilation-error-in-dot-net-core-explain-with-an-example) 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](https://www.mindstick.com/articles/11911/encapsulation)) |
| `protected` | Same package + subclasses | Inheritance (OOP concept) |
| Default | Same package only | Package-level [access control](https://answers.mindstick.com/qa/112595/how-do-i-integrate-facial-recognition-technology-into-smart-home-security-systems-for-access-control) |

Also, Read: [Explain the Constructors in Java](https://www.mindstick.com/articles/338835/explain-the-constructors-in-java)

---

Original Source: https://www.mindstick.com/articles/338838/explain-access-modifiers-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
