Access modifiers

The access modifiers in java specifies accessibility (scope) of a data member, method, constructor or class.

The four access levels are:
  1. Default (Visible to the package)
  2. Private (Visible to the class only)
  3. Public (Visible to the world)
  4. Protected (Visible to the package and all sub classes)
Access Specifiers in Java

1) Default Access Modifier (No keyword)
  • Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
  • A variable or method declared without any access control modifier is available to any other class in the same package. 
Example:
Variables and methods can be declared without any modifiers, as in the following examples:

1
2
3
4
5
String version ="1.5.1";

boolean processOrder(){
 return true;
}

2) Private Access Modifier
  • Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.
  • Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
  • Variables that are declared private can be accessed outside the class, if public getter and setter methods are present in the class.
  • Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world.
Example:
The following class uses private access control:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class Dog {
 private int Age;

 public int getAge() {
  return this.Age;
 }

 public void setFormat(int Age) {
  this.Age = Age;
 }
}

Here, the Age variable of the Dog class is private, so there's no way for other classes to retrieve or set its value directly.
So to make this variable available to the outside world, we defined two public methods: getAge(), which returns the value of Age and setAge(int), which sets its value.

3) Public Access Modifier
  • A class, method, constructor, interface etc declared public can be accessed from any other class. 
  • Therefore fields, methods, blocks declared inside a public class can be accessed from any class.
  • If you want to access public class from different package, then you have to import the public class.
Example:
The following function uses public access control:

1
2
3
public static void main(String[] arguments){
       // ...
}

The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such as java) to run the class.

4) Protected Access Modifier
  • Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
  • The protected access modifier cannot be applied to class and interfaces.
  • Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class A {
 protected void msg() {
  System.out.println("Hello");
 }
}

class B extends A {
 public static void main(String args[]) {
  B obj = new B();
  obj.msg();
 }
}

Output:Hello
The following table shows the access to members permitted by each modifier.



<-- Previous || Next -->

No comments:

Post a Comment