Access ModifiersLearn about Access Modifier in Java Consider the below scenario in your house, where you do not allow strangers (like a salesman knocking the door) inside your house. So a stranger can know about your house only as much as what he can see from outside. Consider a person like a colleague (whom you are not very close). You may allow them inside your house but not beyond the reception room. If it is your relatives or intimate neighbours they gain access till hall. But you do not allow any of the above said people into your bedroom which is strictly private.
Thus in a similar fashion, in Java we need to secure classes, fields (also called member variables) and methods at different levels depending on the requirement. Hence Java supports the following access levels private, default, protected, public. We will learn in detail about these access modifiers in future chapters.
|