Access modifiers are a set of keywords that determines the visibility and accessibility of classes, methods, and fields. Java provides four access levels correspond to four access modifiers.
- public
- protected
- default
- private
Public access modifier
Same class access | True |
---|---|
Same package subclass access | True |
Same package non-subclass access | True |
Different package subclass access | True |
Different package non-subclass access | True |
Key word: public |
Hi there
Protected access modifier
Same class access | True |
---|---|
Same package subclass access | True |
Same package non-subclass access | True |
Different package subclass access | True |
Different package non-subclass access | False |
Keyword: protected |
Hi there
Default access modifier
Same class access | True |
---|---|
Same package subclass access | True |
Same package non-subclass access | True |
Different package subclass access | False |
Different package non-subclass access | False |
Compile time error
Private access modifier
Same class access | True |
---|---|
Same package subclass access | False |
Same package non-subclass access | False |
Different package subclass access | False |
Different package non-subclass access | False |
Keyword: private |
error: display() has private access in A
obj.display();
Back to parent page: Java Standard Edition (Java SE) and Java Programming
Web_and_App_DevelopmentProgramming_LanguagesJavaAccess_Modifier
Reference