---
title: "What is the difference between protected, public, package-private, and private in Java?"  
description: "What is the difference between protected, public, package-private, and private in Java?"  
author: "Utpal Vishwas"  
published: 2023-07-21  
updated: 2023-07-22  
canonical: https://www.mindstick.com/forum/159216/what-is-the-difference-between-protected-public-package-private-and-private-in-java  
category: "java"  
tags: ["java", "access modifiers"]  
reading_time: 2 minutes  

---

# What is the difference between protected, public, package-private, and private in Java?

What is the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between protected, public, [package](https://www.mindstick.com/blog/12619/tips-for-finding-the-right-package-when-you-buy-instagram-followers)-[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers), and private in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?

## Replies

### Reply by Aryan Kumar

Java has four access modifiers:

- **public**
- **protected**
- **package-private**
- **private**

The access modifier of a class member (e.g., field, method, or constructor) determines whether the member is accessible to other classes.

Here's a table summarizing the difference between the four access modifiers:

| Access Modifier | Visibility |
| --- | --- |
| Public | Visible to all classes, regardless of their package. |
| Protected | Visible to subclasses of the class in which it is declared, and to classes in the same package. |
| Package-private | Visible to all classes in the same package. |
| Private | Visible only to the class in which it is declared. |

Here are some examples of how to use the different access modifiers:

- A `public` class can be used by any class in any package.
- A `protected` class can be used by subclasses of the class in which it is declared, and by classes in the same package.
- A `package-private` class can be used by any class in the same package.
- A `private` class can only be used by the class in which it is declared.

Access modifiers can also be used on class members, such as fields, methods, and constructors. The access modifier of a class member determines whether the member is accessible to other classes.

For example, a `public` field can be accessed by any class, while a `private` field can only be accessed by the class in which it is declared.

Here are some examples of how to use access modifiers on class members:

- A `public` field can be accessed by any class.
- A `protected` field can be accessed by subclasses of the class in which it is declared, and by classes in the same package.
- A `package-private` field can be accessed by any class in the same package.
- A `private` field can only be accessed by the class in which it is declared.

Access modifiers are an important part of Java's object-oriented programming model. They allow you to control the visibility of your class members and classes, which can help you to write more secure and maintainable code.


---

Original Source: https://www.mindstick.com/forum/159216/what-is-the-difference-between-protected-public-package-private-and-private-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
