Home

 › 

Concepts

 › 

Understanding Access Modifiers in Java, With Examples

OOPs in Java

Understanding Access Modifiers in Java, With Examples

Access Modifiers in Java are keywords that manage the visibility and accessibility of fields, classes, and methods. In this article, we will go over all four of them and explain how they can be used.

But first, let’s review what packages are.

What Are Packages?

Packages are a useful way of organizing interfaces and classes in Java. They are often named according to reversed domain names. For example, if you wanted two Java classes MyClass1 and MyClass2 to be in the same package, you could put this at the top of each of them:

package com.example.myPackage;

And the directory structure would look something like:

src/
└── com/
    └── example/
        └── myPackage/
            ├── MyClass1.java
            └── MyClass2.java

Depending on what access modifiers are used, the package that classes are in can greatly affect accessibility throughout the program.

Access Modifiers in Java: Classes

Applying an access modifier to a class looks something like this:

[access_modifier] class ExampleClass {
}

Note that, for the most part, these rules will apply to interfaces as well.

Public

When a class is declared public, it can be accessed from any other class or package, making it the highest level of visibility. It is important to note that there can only be one public class per Java file, and the name of this class must match the name of the file.

Private

A private class can only be accessed inside the same enclosing class. This is typically used for inner classes that assist in the functionality of the outer one. This is the most restrictive modifier, and subclasses or classes in the same package cannot access a class with it.

Protected

When a class is protected, this means that it can be accessed by classes in the same package, as well as subclasses that are not necessarily in the same package.

Default (Package-Private)

A class is considered package-private when no access modifier is specified. If a class is package-private, it can only be accessed by classes in the same package. This is especially useful when it is not necessary for implementation details to be accessed within a package.

access modifiers in java

This example shows 3 different types of access modifiers in Java: public, private, and protected.

Access Modifiers in Java: Methods

Using access modifiers on methods inside classes looks something like this:

[access_modifier] [return_type] exampleMethod() {

}

Public

Similar to public classes, methods declared as public can be accessed anywhere within the program. Public methods are typically the ones that interface with the bulk of the class’s functionality.

Private

A private method is only accessible within the same class. They are usually created to abstract away implementation details that are not necessary for outside classes.

Protected

Protected methods can be accessed by classes in the same package and subclasses, provided that the class containing the method is accessible.

Default (Package-Private)

A package-private method can only be accessed by classes in the same package, just as when this modifier is used for a class. This is done when certain functionalities only need to be used by other classes in the package.

Access Modifiers in Java: Fields

When you are applying an access modifier to a field inside a class, it looks like:

[access_modifier] [data_type] exampleField;

Public

As expected, public fields can be accessed by any class within a program, as long as the enclosing class is accessible. You should try to avoid public fields, as it is better practice in Java to use getters and setters that promote encapsulation.

Private

A private field can only be accessed in the same class. The majority of the fields in a class will likely be private, as it is not necessary for other classes to know implementation details.

Protected

If a field is protected, then classes in the same package or subclasses can access it. This is most commonly used when you explicitly want fields to be inherited when a subclass is created.

Default (Package-Private)

Although not as common, package-private fields work similar to package-private classes and methods, allowing them to be accessed by classes within the same package only.

Wrapping Up

When you are starting out programming in Java, you don’t need to worry too much about what access modifiers you are applying, as long as they don’t cause problems. However, with larger projects, encapsulation and abstraction become more and more important, and you must use them to their fullest extent. 

Learn fully what each modifier does, and your personal and professional projects will be better organized and maintainable. Now, let’s dive into some frequently asked questions.

Frequently Asked Questions

How to decide access modifiers in Java?

It all comes dow to the level of visibility you want for your classes, methods, and fields. If you want them to be accessible everywhere, use ‘public’. For access only within the same class, use ‘private’. If you want access within the same package and subclasses, use ‘protected’. If you want access only within the same package, you can use the default package-private level by not specifying any modifier.

What is the default access modifier in Java?

In Java, when you don’t specify an access modifier, it defaults to package-private. This means the class, method, or field is accessible to all classes within the same package.

What is public and private access specifiers in Java?

‘Public’ and ‘Private’ are access specifiers in Java. ‘Public’ means that the class, method, or field can be accessed from anywhere in the application. ‘Private’, on the other hand, restricts access only to the class where it is defined.

What is the 'protected' access modifier in Java?

The ‘protected’ access modifier in Java allows a class, method, or field to be accessed from within the same package and also by subclasses, even if they are in different packages.

To top