Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Access Modifiers

Access Modifiers provides certain level of restriction to the usage of the class members so that they are not accidentally modified.

Let us understand it in detail.

The main motto of using Classes and Objects is to maintain certain standards. So, that the data inside the objects do not get modified or deleted accidentally.

i.e. Let us consider the class 'Human'.


class Human {

   int age;

   public String getAge() {
    return age;
   }

   public void setAge(String age) {
    this.age = age;
   }
}


The above class 'Human' has an attribute called 'age'. And we are using a setter method


public void setAge(String age) {
  this.age = age;
}

to set some value to the age.

And we have a getter method


public String getAge() {
  return age;
}


to get the age.

So, we do not access the data members (i.e. 'age') directly outside the class. We need to use a getter and setter method to do that.

And this is where 'Access Modifiers' comes into picture. It puts a restriction on the variables.

Let us see each in detail :


Private Access Modifier

A Private access modifier will never allow a class member to be accessed outside the class.Usually, an attribute is marked as private(i.e. age in the above class). So, that they should not be accessed outside the class.

A private data member can be accessed only inside a class.


class Human {

   private int age;

   public String getAge() {
    return age;
   }

   public void setAge(String age) {
     this.age = age;
   }
}

Public Access Modifier

A Public data member can be accessed outside the class. Usually, methods in a class are Public and attributes in a class are private.

Also, a class can be declared public, so that this class is accessible and visible to all the classes in all the packages in Java.

But if a public class you are trying to access, is in a different package. You need to import that class in your package.

If you are declaring an attribute of a class public. You can access it from outside the class. No getter and setter method is needed for that. But that's never a good practice.


class Human {

  public int age; // Not a good practice but allowed.

}

Protected Access Modifier

1.   A Protected data member can be accessed outside the class, but in the same package.

2.   But if you want to access it from a class which doesn't belong to the same package, you need to use inheritance.

Let us understand with the below example :

1.   Example with the protected data members are in the same Package.


Animal.java

package myownpackage;

class Animal{

   protected void legs(){

     System.out.println("An animal has 4 legs.");
   }

}

Main.java

package myownpackage;

class Main {

   public static void main(String[] arg) {

     Animal animal = new Animal()

     animal.legs();
   }
}

Since, in the above example both Animal.java and Main.java were in the same package, we were able to access the 'protected' method legs(),


protected void legs(){

  System.out.println("An animal has 4 legs.");
}

from the 'Main' class.


animal.legs();

2.    Example with the protected data members present in different Package.


Animal.java

package myownpackage;

class Animal{

   protected void legs(){

     System.out.println("An animal has 4 legs.");
   }
}

Main.java


package mainpackage;
import myownpackage.Animal;

class Main extends Animal{

   public static void main(String[] arg) {

     Animal animal = new Animal();

     //animal.legs();// Not allowed

     Main main = new Main();

     main.legs();
   }
}

Now, since the Animal class and Main class are in different package. We cannot access the protected method 'protected void legs()' from the 'Main' class.

So, what we do is :


1. Extend Animal class, so that all the protected methods gets inherited. Then access them using an object of the base class.


  class Main extends Animal {

     ...
     ...
     Main main = new Main();
     main.legs();
     }
}

Default access Modifier

So, it is when we don't specify anything. The default modifier is placed by the compiler by default. The variables and methods with default modifiers can only be accessed inside the package.

It behaves like a public modifier, but inside a package.


class Human {

   int age;

   public String getAge() {
     return age;
   }

   public void setAge(String age) {
    this.age = age;
   }
}

In the above example, since we have not specified any access modifier in the 'age' attribute


int age;

It is the default modifier.


Let us have a look at the below table, which says when and where we can use a particular access modifier.


Access Modifier In a class In a package outside the package but by subclass outside of package
Private Yes No No No
Default Yes Yes No No
Protected Yes Yes Yes No
Private Yes No No No
Public Yes Yes Yes Yes