In last lesson we were talking about classes and we have created four variables or properties of the class (did you remember that). We have used the word "private" before them. What was that. This was the access modifier. What we are asking to our program is that no one can use this variable or properties except Student class. So only Student class methods have access to use them. The benefit of this privacy is security and this technique is less prone to error. Changes and up gradation is easy. Because you don't need to worry about the other class and their methods.
Java has two other access modifier too. Which are "public" and "protected". All three keyword can be used with classes, methods and class members (class variables). By using these you can restrict the user of the class to use only provided methods to access them.
If you don't use any modifier then default access rule will be used. That is you can use that member in same package anywhere.
Public: If you use public keyword, it will be available for all other class that are included in your project. The use of public in class members is not appreciated so you must avoid this practice.
Private: As the name indicates, private member are only available to the same class member functions or methods.
Protected: Protected members of the super class are available to the sub class of the same package.
So guys these were the access modifiers. If you have questions or suggestions please do write us.
No comments:
Post a Comment