Before we proceed further in developing programs in java lets cover access modifiers in details.They are quite simple to understand.
There are four access modifiers in JAVA.They define who can access the corresponding variables or functions.
There are four access modifiers in JAVA.They define who can access the corresponding variables or functions.
- Public
- Protected
- No access modifier
- Private
- Class level access modifier
- Member level access modifier.
Class level Access modifier
There are two access modifiers at class level
- public - If class is defined as public then the class can be accessed from anywhere.
- No access modifier - If no access modifier is specified class can be accessed only in the same package.
Member level Access modifier
When i say a member in Java, a member can be a member variable or a member function.All four access modifiers are applicable in this case.
- private - If a member is defined to be private it can only be accessed by a member in the same class and nowhere else.
- No access modifier - If no access modifier is specified that member can be accessed by a member in the same class as well as member in classes within the same package but not by members of subclass or members outside current package.
- Protected - If a member is defined as protected it can be accessed by a member in same class, in same package and in all of it's subclasses but not by members in other packages.
- public - A public member can be accessed by any member from anywhere.
No comments:
Post a Comment