Access Modifiers in Java

Access Modifiers in Java


In Java programming language we have two types of modifiers that are access modifiers and non access modifiers.

The access modifiers are used to specify the scope of the data members and member functions and constructors or class etc.

In java programming language there are four different types of access modifiers:

  1. private
  2. default
  3. protected
  4. public

The non access modifiers include static, abstract, volatile, etc.


Private access modifiers:

The private access modifier can be accessed within a class only.


Simple example of private access modifier:

Consider the following example in which two classes are declared:

CODE:

class B {

private int id = 122;

private void display () {

system. out. println (“Hello World”);

}

public class Example {

public static void main (string args []) {

B b1 = new B ();

system. out. println (b1. id);

b1. display ();

}

}

OUTPUT:

Compile time error

We got an error because the method and data member of the first class are kept private by using the private keyword and when we tried to access them through another class the compiler gave error.


Role of private constructor:

If the constructor of any class is made private then instance of that class from outside the class cannot be created. Consider the following example:

CODE:

class B {

private b () {}

void display () {

system. out. println (“Hello World”);

}

public class Example {

public static void main (string args []) {

B b1 = new B ();

}

}

OUTPUT:

Compile time error

We got a compilation error because constructor is private and when the object is created the constructor is called automatically which caused an error. Any class can never be private or protected; it can only be public except the nested class.


Default access modifier:

If no modifier is used then it will be considered as the default modifier by default. The default modifier in java language is accessible to only package.


Example of Default access modifier:

Consider the following example in which two packages are created:

CODE:

package A;

class B {

void display () {

system. out. println (“Hello World”);

}

}

package S;

import A.*;

class C {

public static void main (string args []) {

B b1 = new B ();

b1. display ();

}

}

OUTPUT:

Compile time error

In the above example we got a compilation error because the scope of the class B and its method is default hence we cannot access them from outside the package.


Protected access specifier:

The protected access specifier is that which can be accessed within the package and from outside the package also. It can be accessed from outside the package when we are using inheritance. We can apply the protected access specifier on data members, methods and constructors. The protected access specifier cannot be applied to class.


Example of protected access specifier:

Consider the following example in which two packages are created:

CODE:

package A;

public class B {

protected void display () {

system. out. println (“Hello World”);

}

}

package S;

import A.*;

class C extends B {

public static void main (string args []) {

C c1 = new C ();

c1. display ();

}

}

OUTPUT:

Hello World

In the above example two packages are created and the class B of package A is public, therefore, we can access this class from outside the package. The display () method of this package is declared as protected. This method can be accessed from outside the package only if we use inheritance.


Public access modifier:

When a method, class or data member is declared as public it can be accessible from everywhere.


Example of public access specifier:

Consider the following example in which two packages are created:

CODE:

package A;

public class B {

public void display () {

system. out. println (“Hello World”);

}

}

package S;

import A.*;

class C {

public static void main (string args []) {

B b1 = new B ();

b1. display ();

}

}

OUTPUT:

Hello World


Understanding all Java access modifiers:

Consider the following table and you will understand the difference between the four access modifiers:

Access Modifier Scope within class Scope within package Scope outside package by sub class only Scope outside package
Private Yes No No No
Default Yes Yes No No
Protected Yes Yes Yes No
Public Yes Yes Yes Yes


Java access specifiers with method overriding:

If methods are overridden in a program then they should not be restricted that is they should not be protected. The default access modifier is more restrictive than a protected modifier and hence the overridden methods should not be declared as protected or as default.

Consider the following example:

CODE:

class B {

protected void display () {

system. out. println (“Hello World”);

}

}

public class Example extends B {

void display () {

system. out. println (“Hello Java”);

}

public static void main (string args []) {

Example a1 = new Example ();

a1. display ();

}

}

OUTPUT:

Compile time error

In the above example we got a compilation error because the method display () that was overridden was declared as protected in the class B and therefore, compile time error occurred.