Access modifiers in java

Let’s learn access modifiers in java.

Access modifiers in java

Java access modifiers define the limit or scope of a variable, constructor, class or a method.

In my previous posts you have seen the use of public, private and protected keywords. These are access modifiers.

Also read – constructor in java

There are two types of modifiers in java; access modifiers and non-access modifiers.

There are four types of access modifiers in java; private access modifier, protected access modifier, public access modifier and default access modifier.

private access modifier

Varaible or method declared as “private” can be accessed only within the same class in which they are declared.

It cannot be accessed outside the class of same package.

Also read – class and objects in java

Private access modifiers are restrictive one. Because it restricts the access of variables or methods outside the package.

NOTE:

Class or interface cannot be declared as private.

Example of private access modifier:

class Demo
{
   private void display()
   {
      System.out.println("Hello world java");
   }
}
public class PrivateAccessModifierExample
{
   public static void main(String[] args)
   {
      Demo obj = new Demo();
      System.out.println(obj.display());
   }
}


Output:

Compile-time error: The method display() from the type Demo is not visible.


public access modifier

Class or method or variable declared as “public” can be accessed globally or everywhere.

Also read – nested classes in java

If we want to access a class which is “public” outside the package then we can “import” that class to particular package.

Example of public access modifier:

package a1;
public class Example1
{
   public void printName()
   {
      System.out.println("Flower Brackets");
   }
}
package a2;
import a1.*;
public class PublicAccessModifierDemo
{
   public static void main(String[] args)
   {
      Example1 obj = new Example1();
      obj.printName();
   }
}


Output:

Flower Brackets


protected access modifier

Data members or methods or variables declared as “protected” can be accessed within same package or subclasses in other package.

Also read – inheritance in java

A class cannot be protected. protected modifier can be applied on constructor, data members and methods.

Example of protected access modifier:

package abc;
class Demo
{
   protected void printName()
   {
      System.out.println("Flower Brackets");
   }
}
package def;
import abc.*;
class ProtectedExample extends Demo
{
   public static void main (String args[])
   {
      ProtectedExample obj = new ProtectedExample();
      obj.printName();
   }
}


Output:

Flower Brackets


default access modifier in java

A data member, method or class specified with no access modifier is treated as default access modifier by default.

Data members, methods or variables having default access modifier can be accessed only within the package.

Example of default access modifier:

package abc;
class Demo
{
   void printName()
   {
      System.out.println("Flower Brackets");
   }
}
package def;
import abc.*;
class DefaultExample
{
   public static void main (String args[])
   {
      // since class Demo is not public we cannot access from outside the package
      Demo obj = new Demo();
      obj.printName();
   }
}


Output:

Compile-time error


Here’s the priority of access modifiers in java.

access modifiers in java

(Least priority) private <<< default <<< protected <<< public (most accessed).


Non-access modifiers in java

Non-access modifiers are used with constructor, classes and methods.

Non-access modifiers are final, static, abstract, strictfp, native, synchronized, transient and volatile.

final non-access modifier: A class declared final cannot be subclassed. A method declared final cannot be overridden by any subclass.

A final variable initialized once, its value can’t be changed.

Example for final non-access modifier:

public static final int a = 5;


abstract non-access modifier: An abstract class has abstract methods. If a class has atleast one abstract method then the class is an abstract class.

An abstract method is a method which do not have body. abstract non-access modifier is applicable to class and method.

Also read – abstraction in java

An abstract method do not have body. Body is provided by subclass. Let’s see an example on abstract non-access modifier.

import java.util.*;
// abstract class
abstract class Person
{
   public String empName = "Virat";
   public int empAge = 23;
   // abstract method
   public abstract void project();
}
// subclass
class Employee extends Person
{
   public int joiningYear = 2005;
   public void project()
   {
      // abstract method body
      System.out.println("working on application development.");
   }
}
public class AbstractExample
{
   public static void main(String[] args)
   {
      Employee obj = new Employee();
      System.out.println("Employee name: " + obj.empName);
      System.out.println("Age: " + obj.empAge);
      System.out.println("Joined year: " + obj.joiningYear);
      // calling abstract method
      obj.project();
   }
}


Output:

Employee name: Virat
Age: 23
Joined year: 2005
working on application development.


Synchronized non-access modifier: synchronized non-access modifier can only be applied for methods.

A method with synchronized non-access modifier will allow only one single thread to execute code at a given time.

Example:

public synchronized void displayVolume() 
{
   
}



Native non-access modifier:

native can only be applied to methods.

Native methods specify that method is implemented on platform dependent code.


Strictfp non-access modifier: when declared inside class will comply to IEEE754 standard.

This makes class to behave in platform independent way regarding floating points.

Strictfp non-access modifier is applicable to classes and methods.


static non-access modifier: static methods belongs to class rather than an object.

Learn more on – static keyword in java


transient non-access modifier: marks an instance variable not to be serialized when it is persisted to streams of bytes.

public transient int a = 50;


volatile non-access modifier: can only be applied to instance variables.

a variable declared volatile will never be cached thread-locally and is always read from the main memory.