---
title: "Visibility Modes and Access Modifiers in Java"  
description: "We know java uses four access modifiers public, protected, default and private to maintain visibility at various levels. Let’s discuss how to define t"  
author: "Simond Gear"  
published: 2016-05-17  
updated: 2018-03-14  
canonical: https://www.mindstick.com/blog/11096/visibility-modes-and-access-modifiers-in-java  
category: "java"  
tags: ["java", "class"]  
reading_time: 3 minutes  

---

# Visibility Modes and Access Modifiers in Java

We know java uses four [access modifiers](https://www.mindstick.com/articles/338838/explain-access-modifiers-in-java) public, protected, default and private to maintain visibility at various levels. Let’s discuss how to define this visibility. Four [possible values](https://www.mindstick.com/forum/23337/hibernate-hbm2ddl-auto-possible-values-and-what-they-do) [control the visibility](https://www.mindstick.com/forum/158658/what-is-the-css-property-used-to-control-the-visibility-or-hidden-state-of-an-element) or the access level of the entities in [your program](https://answers.mindstick.com/qa/33494/how-do-you-find-any-view-element-into-your-program-explain-for-example):

**1. private:** If an entity is declared with a private modifier, it will be accessible only to the code that is contained within the class that defines the entity.

**2. protected:** The entity can only be accessed by code that is contained

**a.** within the class that defines the entity,

**b.** by classes within the same package as the defining class,

**c.** or by a subclass of the defining class, regardless of the package in which the subclass is declared.

\
**3. default (or package):** The entity can be accessed by code that is contained

**a.** within the class that defines the entity

**b.** or by a class that is contained in the same package as the class that defines the entity.

**4. public:** The entity can be accessed by code in any class.

These visibility modes are specified with specific [keywords](https://www.mindstick.com/articles/12899/how-to-spot-if-you-re-optimizing-for-the-wrong-keywords): public, protected, and private. If any of these keywords are not used as qualifiers, the entity is given a default visibility (that is, package-private or [friendly](https://www.mindstick.com/articles/116104/how-to-write-good-seo-friendly-articles)). The four access modifiers and the resulting corresponding visibility are summarized in table below.

Note that there is no modifier called “**default**” If we do not specify any of the modifiers explicitly, the default is assumed. To understand these [accessibility](https://www.mindstick.com/blog/304975/why-image-alt-text-matters-for-seo-and-accessibility) rules, we must first understand the levels at which this visibility is tested.

##### Accessibility is decided at five different levels:

**· Same class:** [Check whether](https://www.mindstick.com/forum/157543/write-a-java-program-to-check-whether-a-string-is-a-palindrome) the element is accessible to the code defined within the same class where the element is declared.

**· Same package subclass:** The classes are grouped together in a package. We test the visibility for access within a subclass declared in the same package.

**· Same package non-subclass****:** We test the visibility within a class that belongs to the same package but does not inherit from the declaring class.

**· Different package subclass:** We test the visibility within a class that inherits from a class belonging to a different package.

**· Different package non-subclass:** In this case, the class neither derives from the declaring class nor belongs to the same package as the declaring class.

|  |  | ## Same Packages | ## Different Packages |  |  |
| --- | --- | --- | --- | --- | --- |
| ## Modifers | ## Same Class | ## [Sub Class](https://answers.mindstick.com/qa/92538/what-are-the-base-class-sub-class-and-superclass) | ## Nonsub Class | ## Sub Class | ## Nonsub Class |
| public | Yes | Yes | Yes | Yes | Yes |
| private | Yes | No | No | No | No |
| protected | Yes | Yes | Yes | Yes | No |
| Default | Yes | Yes | Yes | No | No |

---

Original Source: https://www.mindstick.com/blog/11096/visibility-modes-and-access-modifiers-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
