Skip to main content

Access Modifiers

  • Access modifiers can be used to specify the scope or visibility of the class or members of the class.
  • There are three access modifiers:
    • private
    • protected
    • public

Four Scopes Available

Scope or VisibilityModifier
Private Scopeprivate
Default ScopeNo Modifier
Protected Scopeprotected
Public Scopepublic

Private Scope

  • When you are using private modifier with the members of the class, then the scope of those members will be private scope.
  • Private scope is also called class scope, i.e., private members must be accessed from the same class where it is declared.
  • Private members cannot be accessed from outside the class, not even from subclass, i.e., private members will not be inherited to subclasses.
  • private modifier can be used only for members of the class, not for the class itself.

Default Scope

  • When you are not using any modifier with the members of the class, then the scope of those members will be default scope.
  • Default scope is also called package scope, i.e., default members can be accessed from:
    • same class
    • subclass
    • non-subclass which are available in the same package.
  • Default members cannot be accessed from outside the package, i.e., default members will not be inherited to subclasses available outside the package.
  • Default scope is available for both classes and their members.

Protected Scope

  • When you are using protected modifier with the members of the class, then the scope of those members will be protected scope.
  • Protected members can be accessed from:
    • same class
    • subclass
    • non-subclass which are available in the same package.
  • Protected members can be accessed from the subclasses which are available in different packages.

Scope Visibility Diagram

Quick Comparison Table

ModifierSame ClassSame PackageSubclass Outside PackageEverywhere
private✅❌❌❌
default✅✅❌❌
protected✅✅✅❌
public✅✅✅✅

Protected Scope

  • Protected members cannot be accessed from the non-subclasses which are available in different package.
  • protected modifier can be used only for members of the class, not for the class itself.

Public Scope

  • When you are using public modifier with the members of the class, then the scope of those members will be public scope.
  • Public members can be accessed from anywhere.
  • public modifier can be used for both classes and their members.

A.java

packagecom.javaworld.p1;

public class A {

private int a=10;
int b=20;
protected int c=30;
public int d=40;

public void showA() {

System.out.println("A -> show()");

System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}

B.java

packagecom.javaworld.p1;

public class B extends A {

public void showB() {

System.out.println("B -> show()");

// System.out.println(a);

System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}

C.java

packagecom.javaworld.p1;

public class C {

public void showC() {

System.out.println("C -> show()");

A a1=new A();

// System.out.println(a1.a);

System.out.println(a1.b);
System.out.println(a1.c);
System.out.println(a1.d);
}
}

D.java

packagecom.javaworld.p2;

import com.javaworld.p1.A;

public class D extends A {

public void showD() {

System.out.println("D -> show()");

// System.out.println(a);

// System.out.println(b);

System.out.println(c);
System.out.println(d);
}
}

E.java

packagecom.javaworld.p2;

import com.javaworld.p1.A;

public class E {

public void showE() {

System.out.println("D -> show()");
}

A a1=new A();

// System.out.println(a1.a);

// System.out.println(a1.b);

// System.out.println(a1.c);

System.out.println(a1.d);
}

Access Modifier Behavior Summary

ModifierSame ClassSame Package SubclassSame Package Non-SubclassDifferent Package SubclassDifferent Package Non-Subclass
private✅❌❌❌❌
default✅✅✅❌❌
protected✅✅✅✅❌
public✅✅✅✅✅

Lab486.java

packagecom.javaworld.p3;

import com.javaworld.p1.*;
import com.javaworld.p2.*;

public class Lab486 {

public static void main(String[] args) {

A ob1=new A();
ob1.showA();

B ob2=new B();
ob2.showB();

C ob3=new C();
ob3.showC();

D ob4=new D();
ob4.showD();

E ob5=new E();
ob5.showE();
}
}

Hello.java

packagecom.javaworld.p1;

public class Hello {

protected int ab=99;
}

Lab487.java

packagecom.javaworld.p2;

import com.javaworld.p1.Hello;

class Xyz extends Hello {
}

class Hai extends Hello {

void show() {

System.out.println("Hai -> show()");

Hello h=new Hello();

// System.out.println(h.ab);

Xyzre f=new Xyz();

System.out.println(ref.ab);
}
}

class Lab487 {

public static void main(String args[]) {

Hai hai=new Hai();

hai.show();
}
}

Lab488.java

class Lab488 {

public static void main(String args[]) {
A aobj = new A();

aobj.x = 99;

System.out.println(aobj.x);
}
}

class A {

private int x;
}

Result​

  • Compilation Error
  • Cannot access private member x directly outside the class.

Lab489.java

class Lab489 {

public static void main(String args[]) {
A aobj = new A();

aobj.show();
}
}

class A {

private int x;

void show() {
System.out.println(x);
}
}

Key Points

Protected Member Access​

  • Protected members can be accessed:
    • Inside the same package
    • Inside subclasses
    • Through subclass objects
  • Protected members cannot be accessed using superclass object reference from different package.

Private Member Access​

  • Private variables cannot be accessed directly outside the class.

Example:

aobj.x=99;// Error
  • Private members can be accessed indirectly using methods.

Example:

aobj.show();

Protected Access Flow

Lab490.java

class Lab490 {

public static void main(String args[]) {
A aobj = new A(99);

aobj.show();
}
}

class A {

private int x;

public A(int x) {
this.x = x;
}

void show() {
System.out.println(x);
}
}

Lab491.java

class Lab491 {

public static void main(String args[]) {
A aobj = new A();

aobj.setX(99);

System.out.println(aobj.getX());
}
}

class A {

private int x;

public void setX(int x) {
this.x = x;
}

public int getX() {
return this.x;
}
}

Lab492.java

class Lab492 {

public static void main(String args[]) {
B bobj = new B(99);

System.out.println(bobj.getX());
}
}

class A {

private int x;

A(int x) {
this.x = x;
}

int getX() {
return this.x;
}
}

class B extends A {

B(int x) {
super(x);
}
}

Encapsulation Notes

Constructor Initialization​

Private variables can be initialized using constructors.

Example:

A aobj=new A(99);

Getter and Setter Methods​

Private variables are commonly accessed using:

  • Setter methods → to assign values
  • Getter methods → to retrieve values

Example:

aobj.setX(99);
System.out.println(aobj.getX());

Access Restriction Order

From more restrictive to less restrictive:

Quick Summary Table

ScopeAccessibility
PrivateSame class only
DefaultSame package
ProtectedSame package + subclasses
PublicEverywhere

Important Concepts

Private Variable Access​

private int x;
  • Cannot be accessed directly outside the class.
  • Can be accessed using:
    • Constructors
    • Getter methods
    • Setter methods
    • Internal class methods

Inheritance with Private Members​

  • Private variables are not directly inherited.
  • Subclasses access them indirectly through:
    • Constructors
    • Public/Protected methods

Example:

super(x);