Saturday 2 March 2013

Access modifiers in Java

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.
  • Public
  • Protected
  • No access modifier
  • Private
There are two levels of access modifiers.
  • Class level access modifier
  • Member level access modifier.

Class level Access modifier

There are two access modifiers at class level
  1. public - If class is defined as public then the class can be accessed from anywhere.
  2. 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.
  1. private - If a member is defined to be private it can only be accessed by a member in the same class and nowhere else.
  2. 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.
  3. 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.
  4. public - A public member can be accessed by any member from anywhere.

Table representation for quick review

 

No comments:

Post a Comment

t> UA-39527780-1 back to top