Access Modifiers
Also called visibility modes, access modifiers dictate the accessibility of members (attributes and methods) of a class, especially in inheritance relationships.
There are typically three visibility modes:
1. Public
- Public members are accessible from outside the class as well as by subclasses.
- Inheritance does not alter the accessibility of public members.
- They remain accessible in subclasses.
2. Protected
- Protected members are accessible within the class itself and by its subclasses.
- Subclasses can access protected members inherited from the superclass.
- They are not accessible outside the class hierarchy.
3. Private
- Private members are only accessible within the class where they are defined.
- Inheritance does not grant access to private members in subclasses.
- They cannot be accessed directly by subclasses.