Visibility Modifiers
Learn how Java visibility modifiers help enforce software architecture boundaries by controlling class accessibility within packages. Understand the importance of the package-private modifier to group classes into cohesive modules while preventing unwanted dependencies. Discover practical examples in a hexagonal architecture context to keep code maintainable and secure.
We'll cover the following...
Let’s start with the most basic tool that Java provides us for enforcing boundaries: visibility modifiers.
Visibility modifiers
Visibility modifiers have been a topic in almost every entry-level job interview I have conducted in the last couple of years. I would ask the interviewee which visibility modifiers Java provides and what the differences between them are.
Most of the interviewees only list the public, protected, and private modifiers. Almost none knows the package-private (or “default”) modifier. This is always a welcome opportunity for me to ask some questions about why such a visibility modifier would make sense in order to find out if the interviewee could abstract from their previous knowledge.
So, why is the package-private modifier such an ...