Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Class Accessibility [duplicate]

Slightly related to my other question: What is the difference between the following:

private class Joe
protected class Joe
public class Joe
class Joe

Once again, the difference between the last 2 is what I'm most interested in.

like image 841
Claudiu Avatar asked Dec 06 '25 06:12

Claudiu


2 Answers

A public class is accessible to a class in any package.

A class with default access (class Joe) is only visible to other classes in the same package.

The private and protected modifiers can only be applied to inner classes.

A private class is only visible to its enclosing class, and other inner classes in the same enclosing class.

A protected class is visible to other classes in the same package, and to classes that extend the enclosing class.

like image 80
erickson Avatar answered Dec 07 '25 20:12

erickson


  • private: visible for outer classes only
  • protected: visible for outer classes only
  • public: visible for all other classes
  • class: package-private, so visible for classes within the same package

See JLS for more info.

like image 25
Daniel Hiller Avatar answered Dec 07 '25 19:12

Daniel Hiller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!