Access Control: Data Hiding and Encapsulation
• Java provides control over the visibility of variables and methods.
• Encapsulation, safely sealing data within the capsule of the class Prevents programmers from relying on details of class implementation, so you can update without worry
• Helps in protecting against accidental or wrong usage.
• Keeps code elegant and clean (easier to maintain)
Access Modifiers: Public, Private, Protected
• Public: keyword applied to a class, makes it
available/visible everywhere. Applied to a method or variable, completely visible.
• Default(No visibility modifier is specified): it behaves like public in its package and private in other packages.
• Default Public keyword applied to a class, makes it available/visible everywhere. Applied to a method or variable, completely visible.
• Private fields or methods for a class only visible within that class. Private members are not visible within subclasses, and are not inherited.
• Protected members of a class are visible within the class, subclasses and also within all classes that are in the same package as that class.