Friday, 31 May 2013

Access Modifiers in JAVA

The access to classes, constructors, methods and fields are regulated using access modifiers i.e. a class can control what information or data can be accessible by other classes. Access modifiers specifies who can access them. There are four access modifiers used in java as follows: 
  1. public
  2. private 
  3. protected
  4. no modifer (declaring without an access modifer).
public access modifier: Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package.

private access modifier: The private (most restrictive) fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods or constructors declared private are strictly controlled, which means they cannot be accesses by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter methods for them.

protected access modifier: The protected fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.

default access modifier: Java provides a default specifier which is used when no access modifier is present. Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package. The default modifier is not used for fields and methods within an interface.

Usage of these access modifiers is restricted to following two levels:
  1. class level access modifiers
  2. member level access modifiers.
Class level access modifiers (java classes only):

Only two access modifiers is allowed, public and no modifier for classes in java.

If a class is ‘public’, then it can be accessed from anywhere.
If a class has ‘no modifer’, then it can only be accessed from ‘same package’.

Member level access modifiers (java variables and java methods)

All the four public, private, protected and no modifer is allowed.

public and no modifier – the same way as used in class level.
private – members can only access.
protected – can be accessed from ‘same package’ and a subclass existing in any package can access.

For better understanding, member level access is formulated as a table:
Access Control and Inheritance: Following are the rules for inherited methods are enforced:
  • Methods declared public in a superclass also must be public in all subclasses.
  • Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.
  • Methods declared without access control (no modifier was used) can be declared more private in subclasses.
  • Methods declared private are not inherited at all, so there is no rule for them.
Example:  The following figure shows the four classes in this example and how they are related.
The following table shows where the members of the Class1 class are visible for each of the access modifiers that can be applied to them.

We will look example in next post(please see Access Modifiers Example post).

No comments:

Post a Comment