gotchajavaCritical
What is the difference between public, protected, package-private and private in Java?
Viewed 0 times
privatepackagejavaandprotectedbetweenpublicthedifferencewhat
Problem
In Java, are there clear rules on when to use each of access modifiers, namely the default (package private),
public, protected and private, while making class and interface and dealing with inheritance?Solution
The official tutorial may be of some use to you.
Class
Package
Subclass
(same pkg)
Subclass
(diff pkg)
World
✅
✅
✅
✅
✅
✅
✅
✅
✅
❌
no modifier
✅
✅
✅
❌
❌
✅
❌
❌
❌
❌
✅: accessible
❌: not accessible
Class
Package
Subclass
(same pkg)
Subclass
(diff pkg)
World
public✅
✅
✅
✅
✅
protected✅
✅
✅
✅
❌
no modifier
✅
✅
✅
❌
❌
private✅
❌
❌
❌
❌
✅: accessible
❌: not accessible
Context
Stack Overflow Q#215497, score: 6452
Revisions (0)
No revisions yet.