Access Modifiers in Java

Access Modifers in Java Access Modifiers in Java

In this tutorial, we will discuss about the Access modifiers in java. Access modifiers are important part of any java application. Access modifiers need to be used carefully in your application. Your application data visibility depends on these modifiers. If not used carefully, unauthorized entity can access your private information and either read it or modify it.

What is Access Modifier 

Access modifiers allows you to set the scope of a member, method or a Class. They are used to set the accessibility or visibility of our Class, method or data members for outside world.


Before we begin to implement it, let’s first understand what does it mean. Let’s take a real world example to understand it better.

Almost each and every person go for the shopping in different malls. Because we all love shopping. We enter into the mall and we see different shops, where we can go and buy anything we want.

But on some of the places, you are not authorized to enter. There are security staffs, who will not allow you to enter that area like monitor room where they are keep watching on activities  happening in mall. You will not be allowed to enter there.

These security staffs are like access modifiers, we have different types of security staffs in mall like at some places they allow everyone to go, on some places only some people can enter, but on some places nobody is allowed to go.

In java, Access modifiers works like that way. Access modifiers protects you information from outside world.


Types of Access Modifiers and where they can be applied

Access Modifers in Java



From the above table, it is clear how you can access your members, methods with different access modifiers. Now let’s implement them one by one with simple examples.

Default Access Modifier

Default access modifier is provided by Java, in case when you don’t specify any access modifier to class, method or variable. But with the default access modifier, your visibility is limited to withing the same class or same package. Outside the package, you will not be able to access your information. 

Example of using default access modifier

Create package one as below and write some code as below :
package com.mypackage.one;

class AccessModifierExampleOne {
      int a = 10;

      void displayA() {
         System.out.println("Value of a : " + a);
      }

      public static void main(String arg[]) {
         AccessModifierExampleOne one = new AccessModifierExampleOne();
         one.displayA(); // will work
      }
}
Create package two as below and write some code as below :

package com.mypackage.two;

import com.mypakcage.one.*;

class AccessModifierExampleTwo {
      
      public static void main(String arg[]) {
         AccessModifierExampleOne ameo = new AccessModifierExampleOne();
         one.displayA(); // You will get error on compile.
      }
}

Private Access Modifier

Private access modifier when applied to data member or method, they can only be accessed within the same class. You can not access them outside the class, package or in child class.

Note*: Private access modifier can not be applied to Class. Otherwise you will get illegal access modifier exception.

Example of using private access modifier

Create package one as below and write some code as below :
package com.mypackage.one;

class AccessModifierExampleOne {
      private int a = 10;

      void displayA() {
         System.out.println("The value of a is : " + getA());
      }

      private int getA() { return a; }

      public static void main(String arg[]) {
         AccessModifierExampleOne one = new AccessModifierExampleOne();
         one.getA(); // will not work
         one.displayA(); // Will work because of default modifier. 
                         //getA() is private and only accessible inside the same class.
      }
}
Create package two as below and write some code as below :

package com.mypackage.two;

import com.mypakcage.one.*;

class AccessModifierExampleTwo {
      
      public static void main(String arg[]) {
         AccessModifierExampleOne ameo = new AccessModifierExampleOne();
         one.getA(); // You will get error on compile.
      }
}

Protected Access Modifier

Protected access modifier when used with method or data members, you can access them within the same class, same package and outside of the package but by the child class which inheriting the class.

Note*: Protected access modifier can not be used with the class.

Example of using protected access modifier

Create package one as below and write some code as below :
package com.mypackage.one;

class AccessModifierExampleOne {
      private int a = 10;

      protected void displayA() {
         System.out.println("The value of a is : " + getA());
      }

      private int getA() { return a; }

      public static void main(String arg[]) {
         AccessModifierExampleOne one = new AccessModifierExampleOne();
         one.getA(); // will not work, it is private
         one.displayA(); // Will work because of protected modifier. 
                         //getA() is private and only accessible inside the same class.
      }
}
Create package two as below and write some code as below :

package com.mypackage.two;

import com.mypakcage.one.*;

class AccessModifierExampleTwo extends AccessModifierExampleOne {
      
      protected void useParentDisplay() {
         displayA();
      }
      public static void main(String arg[]) {
         AccessModifierExampleOne one = new AccessModifierExampleOne();
         one.displayA(); // Will not work, because it is protected.
         one.useParentDisplay(); // will work, because it can use parent method.
      }
}
Note*: We will discuss about child, parent and inheritance concepts late. But for now, you need to understand that you can inherit a class using extends keyword. All public, protected member and methods can be used by your child class. displayA() is protected method of parent class so it is accessible in child class.

Public Access Modifer

Public access modifier has no restriction. If you apply public access modifier to your Class, method, data member. You can access it anywhere in you application.

Note*: Public access modifier can be applied to class.

Example of using public access modifier

Create package one as below and write some code as below :
package com.mypackage.one;

class AccessModifierExampleOne {
      public int a = 10;

      public void displayA() {
         System.out.println("The value of a is : " + getA());
      }

      public int getA() { return a; }

      public static void main(String arg[]) {
         AccessModifierExampleOne one = new AccessModifierExampleOne();
         one.getA(); // will work
         one.displayA(); // Will work
      }
}
Create package two as below and write some code as below :

package com.mypackage.two;

import com.mypakcage.one.*;

class AccessModifierExampleTwo extends AccessModifierExampleOne {
      
      public void useParentDisplay() {
         displayA();
      }
      public static void main(String arg[]) {
         AccessModifierExampleOne one = new AccessModifierExampleOne();
         one.displayA(); // Will work.
         one.useParentDisplay(); // will work
      }
}

Leave a Reply

Your email address will not be published. Required fields are marked *