Java provides control over the visibilityof variables and methods.
- Helps in protecting against accidental or wrong usage.
- Keeps code elegant and clean (easier to maintain)
Access Modifiers Public Private Protected Default in Java |
Access Modifiers: Public, Private, Protected, Default
- Public:keyword applied to a class, makes it available/visible every where. Applied to a method or variable, completely visible.
- Default Public key word applied to a class, makes it available/visible every where. Applied to a method or variable, completely visible.
- Private fields or methods for a class only visible with in that class. Private members are not visible with in sub classes, and are not inherited.
- Protected members of a class are visible with in the class,sub classes and also within all classes that are in the same package as that class.
- Default(No visibility modifier is specified): it behaves like public in its package and private in other packages.