Nov 2, 2022 1 min read

What is the difference between public, protected, package-private and private in Java?

What is the difference between public, protected, package-private and private in Java?

Checkout Below Table

The official tutorial may be of some use to you.


ClassPackageSubclass
(same pkg)
Subclass
(diff pkg)
World
public+++++
protected++++
no modifier+++
private+

+ : accessible
blank : not accessible

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to BugFix.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.