In this article Access Modifiers in Java we give the information about Access modifiers in Java are used to control the visibility (i.e. who can access it) of a class, method, variable or constructor.

Access Modifiers in Java:

Access modifiers in Java are used to control the visibility (i.e. who can access it) of a class, method, variable or constructor. There are four types of access modifiers in Java:

  1. Private

Personal

It is used when we want a variable or method to be accessible only inside that class.

Example

class Example {

private int age;

private void showAge() {

System.out.println(age);

}

}

Here age and showAge() method will be accessible only inside the Example class.

// Example:

class PrivateExample {

private int data = 40;  // private variable

private void display() {  // private method

System.out.println(“Private Method: ” + data);

}

}

public class TestPrivate {

public static void main(String[] args) {

PrivateExample obj = new PrivateExample();

// An error will occur when accessing obj.data and obj.display().

// System.out.println(obj.data);  // Error

// obj.display();  // Error

}

}

Explanation: data and display() are private, so they cannot be accessed from the TestPrivate class.

  1. Default (no modifier)

No modifier (default)

If no access modifier is specified, it is the default. It is also called package-private.

This modifier makes the variable or method accessible only within that package.

Example:

class Example {

int age;

void showAge() {

System.out.println(age);

}

}

Here age and showAge() method will be accessible in the same package.

//Example:

class DefaultExample {

int data = 50;  // default variable

void display() {  // default method

System.out.println(“Default Method: ” + data);

}

}

public class TestDefault {

public static void main(String[] args) {

DefaultExample obj = new DefaultExample();

System.out.println(obj.data);  // Accessible

obj.display();  // Accessible

}

}

Explanation: data and display() are declared without any modifier, so they are accessible inside the same package.

  1. Protected

reserve

This modifier is accessible within a class, subclass and within the same package.

Example:

class Example {

protected int age;

protected void showAge() {

System.out.println(age);

}

}

age and showAge() methods will be accessible within the subclass and the same package.

// Example:

class ProtectedExample {

protected int data = 60;  // protected variable

protected void display() {  // protected method

System.out.println(“Protected Method: ” + data);

}

}

public class TestProtected extends ProtectedExample {

public static void main(String[] args) {

TestProtected obj = new TestProtected();

System.out.println(obj.data);  // Accessible in subclass

obj.display();  // Accessible in subclass

}

}

Explanation: data and display() are protected, so they are accessible in subclass TestProtected.

 4.Public

Public

It is used when we want the class, method, or variable to be accessible everywhere, no matter what class or package it is in.

Example

class Example {

public int age;

public void showAge() {

System.out.println(age);

}

}

age and showAge() method will be accessible from everywhere.

//Example:

class PublicExample {

public int data = 70;  // public variable

public void display() {  // public method

System.out.println(“Public Method: ” + data);

}

}

public class TestPublic {

public static void main(String[] args) {

PublicExample obj = new PublicExample();

System.out.println(obj.data);  // Accessible

obj.display();  // Accessible

}

}

Explanation: data and display() are public, so they are accessible everywhere.

Summary Table:

Modifier Inside Class In Subclass Inside Package Other Package

Private

Yes No No

No

Default

Yes No Yes

No

Protected

Yes Yes Yes

No

Public Yes Yes Yes

Yes

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *