What is the difference between public, protected, and private in Java?
Tags: java, development, explainlikeimfive, programming
Published: 2018-11-01 16:35:33

---
title: What is the difference between public, protected, and private in Java?
published: true
description: 
tags: java, development, explainlikeimfive, programming
---

#### Why we care

When modifying Java access, the general rule is to use only the minimum level of access that is necessary. Why? The idea is to encapsulate each piece of your system as much as possible. This way, you protect yourself and others from refactors that might have a ripple effect on other parts of the system. So, if you want to allow others to use your BuzzingBees method, but want to be able to modify BeautifulButterflies at your leisure, you can lock access to the butterflies down. You should be able to optimize your own code without breaking other code as much as possible. So, generally you want to start with private and go from there. 

#### So how do we decide? In order of least private to most private: 

Public, the simplest of the modifiers, will make the object most widely accessible. Everything in the module can see something public. It is accessible in all classes. If I was the queen of a kingdom, my castle would be big enough for everyone to see. Anyone could come up to the front gates and request entry to the castle via the bridge. They could even walk through the surrounding garden. But they wouldn’t be able to access the main hall or any of the contents of the castle without permission because these are not public.  

Protected allows less access than public, making the member visible by classes throughout the same package OR any subclasses. This is package scope and child scope--potentially the most confusing level of access. Give protected access if you want to do some internal things that should not be exposed publicly, but still intend for the class to be inherited and potentially overridden by subclasses. If you want to change access from private to protected purely for unit testing, try to document that its functionality is not meant to be overwritten. If a lord or lady who carried the same family crest as me on their flag were to come to the gates of my castle, they would be granted entry immediately. They could come and go as they please in and out of the main hall.

With no modifier (default), a member will be accessible within all classes that are in the same package. This is package scope. Crest-carrying members of my family can be inside the castle, but only higher-ranking members, like the mother of the queen, who have been granted special privileges, may access the library or speak to the guards of the dungeon. 

The private modifier restricts member access to only that specific class. This is also called class scope. It is a good starting place. To understand why, note that changing access from private to protected will not be a breaking change, but going the opposite way could be. Try to start here and add access when necessary. In our analogy, the castle is not completely open to my relatives. I would never allow my mother access to the treasure room, for example. 

So, remember to drink lots of water, wash your face before going to bed, and use the minimum level of access necessary in your java code.

| Modifier    | Class | Package | Subclasses | World |
|-------------|-------|---------|------------|-------|
| public      |   ✅   |    ✅    |      ✅     |   ✅   |
| protected   |   ✅   |    ✅    |      ✅     |   ❌   |
| no modifier |   ✅   |    ✅    |      ❌     |   ❌   |
| private     |   ✅   |    ❌    |      ❌     |   ❌   |