Access Modifiers In Java

Access Modifiers In Java

Access Modifier as name implies is used to show how and where to access the classes and methods in a program. Mostly Programming language have three access modifiers, but java have four access modifiers.
A Java access modifier specifies which classes can access a given class and its fields, constructors and methods. Access modifiers can be specified separately for a class, its constructors, fields and methods.

Java access modifiers are also sometimes referred to in daily speech as Java access specifiers, but the correct name is Java access modifiers.


Classes, fields, constructors and methods can have one of four different Java access modifiers:

1) private
2) default (package)
3) protected
4) public

Each of these Java access modifiers will be covered in the following sections. Assigning an access modifier to a class, constructor, field or method is also sometimes referred to as “marking” that class, constructor, field or method as that which the access modifier specifies.

For example, apply the Java access modifier private to a method would be referred to as marking the method as private means it is called a private method.

Access Level Of Access Modifiers

Understanding All Java Access Modifiers

Level of reached of access modifiers in a simple table.

Access Modifierwithin classwithin packageoutside package by subclass onlyoutside package
PrivateYNNN
DefaultYYNN
ProtectedYYYN
PublicYYYY

Public Access Modifier

It have highest level of accessibility and can be access anywhere, in any package and by any program.

Example Of Public Access Modifier

A.java

public class A {
  public void msg() {
    System.out.println("Hello");
  }
}

B.java

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

Output

Hello

Protected Access Modifier

It can be accessed within a same package or in any other (different) package but can be access only through inheritance.

Example Of Protected Access Modifier

A.java

package pack;
public class A {
  protected void msg() {
    System.out.println("Hello parent");
  }
}

B.java

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

Output

Hello parent

Default Access Modifier

If we does not apply any access modifier it will be called as default access modifier.
for instance void show() this method is treated as default access modifier.
Default access Modifier can be accessed same class or within same package .

Example Of Default Access Modifier

package pack;
class A {
  void msg() {
    System.out.println("Hello default");
  }
}
package mypack;
import pack.*;
class B {
  public static void main(String args[]) {
    A obj = new A(); 
    obj.msg();
  }
}

Output

Hello default

Private Access Modifier

It can be accessed within same class only. It can not be used by other class or package.

Example Of Private Access Modifier

A.java

class A {
  private int data = 50;
  private void msg() {
    System.out.println("Hello java");
  }
}

B.java

public class Simple {
  public static void main(String args[]) {
    A obj = new A();
    System.out.println(obj.data); //Compile Time Error
    obj.msg(); //Compile Time Error
  }
}

Where Access Modifiers Can Be Used?

Class –

Only public access modifier can be used on class.
private can be used in nested class or inner class
methods ,constructors, fields 

All modifiers(public,private,default,protected) can be apply.