Sunday, February 24, 2019

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



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?


Answer



The official tutorial may be of some use to you.




______________________________________________________________
| │ Class │ Package │ Subclass │ Subclass │ World |

| │ │ │(same pkg)│(diff pkg)│ |
|───────────┼───────┼─────────┼──────────┼──────────┼────────|
|public │ + │ + │ + │ + │ + |
|───────────┼───────┼─────────┼──────────┼──────────┼────────|
|protected │ + │ + │ + │ + │ |
|───────────┼───────┼─────────┼──────────┼──────────┼────────|
|no modifier│ + │ + │ + │ │ |
|───────────┼───────┼─────────┼──────────┼──────────┼────────|
|private │ + │ │ │ │ |
|___________|_______|_________|__________|__________|________|

+ : accessible blank : not accessible

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...