cover-photoprofile-photo
Erdem İnanç

Software Developer, Fablab Manager

Java, Spring, Android, Python, Vue, Machine Learning

About

Works for Sabanci Univ Co-Space

MSc at Sabanci University

BSc at ITU

Lives in Istanbul

Many Java developers are trying to understand the difference between access modifiers such as private, public, protected and no-modifier.

______________________________________________________________
|           │ Class │ Package │ Subclass │ Subclass │ World  |
|           │       │         │(same pkg)│(diff pkg)│        |
|───────────┼───────┼─────────┼──────────┼──────────┼────────|
|public     │   +   │    +    │    +     │     +    │   +    | 
|───────────┼───────┼─────────┼──────────┼──────────┼────────|
|protected  │   +   │    +    │    +     │     +    │        | 
|───────────┼───────┼─────────┼──────────┼──────────┼────────|
|no modifier│   +   │    +    │    +     │          │        | 
|───────────┼───────┼─────────┼──────────┼──────────┼────────|
|private    │   +   │         │          │          │        |
|___________|_______|_________|__________|__________|________|
 + : accessible         blank : not accessible

Table Source: https://stackoverflow.com/a/215505/5385988

Table above shows everything needed however, I believe it is easier to keep these explanations in mind below:

Private modifier, hides access from other classes within the package.

Public modifier, exposes access to every other classes independent of package level.

Protected modifier, works like public modifier, it only limits access to world (eg. out of project access)

No-modifier, restricts access from different packages.

Check the official documentation for more information.