---
title: "What are the access modifiers and their scopes in Java?"  
description: "What are the access modifiers and their scopes in Java?"  
author: "Ashutosh Patel"  
published: 2024-07-22  
updated: 2024-07-22  
canonical: https://www.mindstick.com/interview/33965/what-are-the-access-modifiers-and-their-scopes-in-java  
category: "java"  
tags: ["java", "access modifiers", "programming language"]  
reading_time: 5 minutes  

---

# What are the access modifiers and their scopes in Java?

#### Access Modifiers in Java

access modifiers are keywords used to describe the accessibility of classes, variables, methods, and constructors. There are four access modifiers in Java, each with its own scope of visibility.

**public**\
The `public` modifier allows the widest access.\
Public members can be accessed from another class, regardless of relationship to a package or subclass.\
**Example-** `public class MyClass { ... }` .

**protected**\
`Protected` modifier allow subclasses (within the same package or different packages) to access within the same package.\
Without subclasses, classes in packages are inaccessible.\
**Example-** `protected int num`

**default (no modifier)**\
The `default` access modifier (also known as package-private) means that no explicit modifier is used.\
It only allows access to the same package.\
Example: `class AnotherClass { ... }` (without explicit variables) .

**private**\
The `private` modifier only restricts access to the same class.\
This is the most restrictive form of entry.\
Example: `private String name`;

```java
// Example of access modifiers in a class
public class Example {
   public int publicVar; // Public variable
   protected int protectedVar; // Protected variable
   int defaultVar; // Default (package-private) variable
   private int privateVar; // Private variable

   // Constructor with different access modifiers
   public Example() {
       // Constructor code
   }

   protected Example(int protectedVar) {
       // Constructor code
   }

   Example(int defaultVar) {
       // Constructor code
   }

   private Example(int privateVar) {
       // Constructor code
   }

   // Methods with different access modifiers
   public void publicMethod() {
       // Method code
   }

   protected void protectedMethod() {
       // Method code
   }

   void defaultMethod() {
       // Method code
   }

   private void privateMethod() {
       // Method code
   }
}

// Example of access modifiers in a class
public class Example {
   public int publicVar; // Public variable
   protected int protectedVar; // Protected variable
   int defaultVar; // Default (package-private) variable
   private int privateVar; // Private variable

   // Constructor with different access modifiers
   public Example() {
       // Constructor code
   }

   protected Example(int protectedVar) {
       // Constructor code
   }

   Example(int defaultVar) {
       // Constructor code
   }

   private Example(int privateVar) {
       // Constructor code
   }

   // Methods with different access modifiers
   public void publicMethod() {
       // Method code
   }

   protected void protectedMethod() {
       // Method code
   }

   void defaultMethod() {
       // Method code
   }

   private void privateMethod() {
       // Method code
   }
}
```

It is important to check the visibility of classes and their members in Java programs, and to understand and properly use modifying methods.

**Also, Read:** [Explain the difference between == and equals() in Java.](https://www.mindstick.com/interview/33964/explain-the-difference-between-and-equals-in-java)

## Answers

### Answer by Ashutosh Patel

#### Access Modifiers in Java

access modifiers are keywords used to describe the accessibility of classes, variables, methods, and constructors. There are four access modifiers in Java, each with its own scope of visibility.

**public**\
The `public` modifier allows the widest access.\
Public members can be accessed from another class, regardless of relationship to a package or subclass.\
**Example-** `public class MyClass { ... }` .

**protected**\
`Protected` modifier allow subclasses (within the same package or different packages) to access within the same package.\
Without subclasses, classes in packages are inaccessible.\
**Example-** `protected int num`

**default (no modifier)**\
The `default` access modifier (also known as package-private) means that no explicit modifier is used.\
It only allows access to the same package.\
Example: `class AnotherClass { ... }` (without explicit variables) .

**private**\
The `private` modifier only restricts access to the same class.\
This is the most restrictive form of entry.\
Example: `private String name`;

```java
// Example of access modifiers in a class
public class Example {
   public int publicVar; // Public variable
   protected int protectedVar; // Protected variable
   int defaultVar; // Default (package-private) variable
   private int privateVar; // Private variable

   // Constructor with different access modifiers
   public Example() {
       // Constructor code
   }

   protected Example(int protectedVar) {
       // Constructor code
   }

   Example(int defaultVar) {
       // Constructor code
   }

   private Example(int privateVar) {
       // Constructor code
   }

   // Methods with different access modifiers
   public void publicMethod() {
       // Method code
   }

   protected void protectedMethod() {
       // Method code
   }

   void defaultMethod() {
       // Method code
   }

   private void privateMethod() {
       // Method code
   }
}

// Example of access modifiers in a class
public class Example {
   public int publicVar; // Public variable
   protected int protectedVar; // Protected variable
   int defaultVar; // Default (package-private) variable
   private int privateVar; // Private variable

   // Constructor with different access modifiers
   public Example() {
       // Constructor code
   }

   protected Example(int protectedVar) {
       // Constructor code
   }

   Example(int defaultVar) {
       // Constructor code
   }

   private Example(int privateVar) {
       // Constructor code
   }

   // Methods with different access modifiers
   public void publicMethod() {
       // Method code
   }

   protected void protectedMethod() {
       // Method code
   }

   void defaultMethod() {
       // Method code
   }

   private void privateMethod() {
       // Method code
   }
}
```

It is important to check the visibility of classes and their members in Java programs, and to understand and properly use modifying methods.

**Also, Read:** [Explain the difference between == and equals() in Java.](https://www.mindstick.com/interview/33964/explain-the-difference-between-and-equals-in-java)


---

Original Source: https://www.mindstick.com/interview/33965/what-are-the-access-modifiers-and-their-scopes-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
