Java Access Modifiers: public, protected, default, private

Nemanja Žunić
Java Vault
Published in
4 min readMay 2, 2018

In this post, we will cover another of the Basic concepts in Java and OOP in general. If you are a Java dev you (hopefully 😅) already know this stuff. But the reason why I still chose to cover this topic is that it is so crucial. I’ve seen it many times on internship and junior position interviews. Also, Java Modifiers knowledge is a requirement before we go into discussing things like Encapsulation or Design Patterns.

So Java Modifiers, what are they? Modifiers are the keywords we use in Java to describe visibility or a specific behavior. You might have seen keywords such as: public , static , private and such…

We have two groups of modifiers:

  1. Access Modifiers
  2. Other (non-Access) Modifiers

Access Modifiers

In this post, I’ll focus on the first group — Access Modifiers.

In Java, we can define the visibility of class attributes and methods. For this purpose, we have 4 access modifiers at our disposal: public, protected, private, and default (no keyword specified).

public

When we use public modifier there is no restriction to the visibility of a method or attribute. Method or attribute with this modifier can be accessed from any other class in our app. Example:

First, take a look at Person class. It has an attribute height, declared with public access modifier. It also contains sayHeight method with public access modifier. Both attribute and the method should be accessible from the other classes. We show this by calling the method sayHeight on line 10: person.sayHeight(); and accessingheight attribute on line 12: System.out.println(“Person's height is: “ + person.height);from the App class. Also notice that App class does not inherit Person class, nor are they in the same package.

protected

When a method or an attribute of the class has protected modifier, only subclasses or classes in the same package can access that method or attribute. Example:

Here we have Person class in model package. Person has height attribute and sayHeight method both declared with protected access modifier.

Next, take a look at the Developer class. It is located in util package, different one thanPerson class is in. But it also extends Person class. So althoughDeveloper and Person classes are in different packages, Developer class can access methods and attributes from Person class which are declared as protected, we can see this on line 13: System.out.println("Developers height is: “ + height);

Now check out HeightDisplayer class. It is not extending Person class but it is located in model package, same as Person class. This allows HeightDisplayer class to access Person class’ attributes and methods which are declared as protected, example on line 6: person.sayHeight();

default (no modifier specified)

When a method or an attribute has no modifier specified then the default modifier is used. This means that method or attribute can be accessed only by classes in the same package. Using the same example as above:

Both classes are in the same model package. But if we had Developer class that is not in the same package as Person but extends it, we would not be able to access height and sayHeight from Developer. That’s because default modifier forbids accessing attributes and methods from subclasses which are not in the same package as the super-class.

private

Method or attribute with private modifier can be accessed only from the class that contains them. Example:

Here we can see that height attribute has private modifier. That means we can only access it within Person class, line 12: System.out.println("My height is: " + height); Trying to access it from any other class would result in an error.

Class or Interface Modifiers

public and default modifiers can be also used on class and interface definitions. They have the same meaning as when they are used on attributes or methods — to define a visibility of a class or an interface. If a class is not visible from another class, we are not able to use it in any way, even if it has public attributes and methods, they won’t be visible to us because the whole class is not visible to us — it cannot be imported.

Access Modifiers Conclusion

So just to recap all the access modifiers and their meanings :

In the next post, I’ll focus on non-Access Modifiers in Java. Feel free to leave a suggestion or a comment and stay tuned for more…🌞🍹

--

--

Nemanja Žunić
Java Vault

I write sentences that make the magic happen (software developer, basically the same thing).