Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothtechsolutions@gmail.com
Vinoth Tech Solutions
  • Home
  • Tutorials
  • Free Complete QA Video Courses
    • Cypress Automation
    • Cucumber BDD Framework
    • JavaScript for Playwright & Cypress Automation
    • API Manual and Automation Testing using SoapUI
    • Appium 2.0 Mobile Automation
    • DevOps CI CD using Real Time Project Setup
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Playwright Course Curriculum
  • About Me
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • Tech Events & Sessions
  • FAQS
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • XPath Demo Page
    • Python JS Online Compiler
    • Python Tutorials
  • Home
  • Tutorials
  • Free Complete QA Video Courses
    • Cypress Automation
    • Cucumber BDD Framework
    • JavaScript for Playwright & Cypress Automation
    • API Manual and Automation Testing using SoapUI
    • Appium 2.0 Mobile Automation
    • DevOps CI CD using Real Time Project Setup
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Playwright Course Curriculum
  • About Me
    • LinkedIn Profile
    • Placed Students Feedback
    • Online Training Feedback
    • Tech Events & Sessions
  • FAQS
  • Demo Sites
    • Practice Automation
      • Registration Form
      • Transaction Details
      • DropDown
      • Basic Authentication
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
      • Date Picker Calendar
    • E-Commerce Demo Application
    • Healthcare Demo Page
    • Create Account Demo Page
    • XPath Demo Page
    • Python JS Online Compiler
    • Python Tutorials
View Categories
  • Home
  • Tutorials
  • Java
  • What are access modifiers?

What are access modifiers?

What are access modifiers?

Access modifiers in Java control the visibility and accessibility of classes, methods, and variables. They help enforce encapsulation by specifying which parts of your code can access certain elements. Java provides four main access modifiers:

  1. public
  2. protected
  3. default (no modifier)
  4. private

1. Public #

Definition: The public access modifier allows the member (variable, method, or class) to be accessed from any other class or package.

Example:

📄
filename.js
// Public class

public class PublicClass {

    // Public variable

    public int number;

    // Public method

    public void display() {

        System.out.println("Number: " + number);

    }

}

// Another class in the same package or a different package

public class Main {

    public static void main(String[] args) {

        PublicClass pc = new PublicClass();

        pc.number = 10;

        pc.display(); // Accessible and outputs: Number: 10

    }

}

In this example, both the PublicClass and its members (number and display) are public, meaning they can be accessed from any other class.

2. Protected #

Definition: The protected access modifier allows the member to be accessed within its own package and by subclasses (including subclasses in other packages).

Example:

📄
filename.js
// Superclass in package 'pkg1'

package pkg1;

public class SuperClass {

    // Protected variable

    protected int number;

    // Protected method

    protected void display() {

        System.out.println("Number: " + number);

    }

}

// Subclass in the same package

package pkg1;

public class SubClass extends SuperClass {

    void show() {
        
        // Accessible because it's protected
        number = 20; 

        // Accessible because it's protected
        display();   

    }

}

// Class in another package

package pkg2;

import pkg1.SuperClass;

public class AnotherClass extends SuperClass {

    public void test() {

        // Accessible because it's protected and we are in a subclass
        number = 30; 

        // Accessible because it's protected and we are in a subclass
        display();   

    }

}

In this example, number and display are protected, allowing access within the same package and by subclasses.

3. Default (No Modifier) #

Definition: The default access modifier (when no modifier is specified) allows access only within the same package. It is also known as package-private.

Example:

📄
filename.js
// Class with default access in package 'pkg1'

package pkg1;

class DefaultClass {

    // Default variable

    int number;

    // Default method

    void display() {

        System.out.println("Number: " + number);

    }

}

// Class in the same package

package pkg1;

public class Main {

    public static void main(String[] args) {

        DefaultClass dc = new DefaultClass();

        dc.number = 40;

        dc.display(); // Accessible and outputs: Number: 40

    }

}

In this example, DefaultClass and its members are accessible only within the same package because no access modifier is specified.

4. Private #

Definition: The private access modifier restricts access to the member within the same class only. It is the most restrictive access level.

Example:

📄
filename.js
// Class with private members

public class PrivateClass {

    // Private variable

    private int number;

    // Private method

    private void display() {

        System.out.println("Number: " + number);

    }

    // Public method to access private members

    public void setNumber(int num) {

        number = num;

    }

    public void show() {

        display(); // Accessible within the same class

    }

}

public class Main {

    public static void main(String[] args) {

        PrivateClass pc = new PrivateClass();

        pc.setNumber(50);

        pc.show(); // Accessible and outputs: Number: 50

        // The following lines would cause compile-time errors

        // Error: number has private access in PrivateClass
        // pc.number = 60; 

        // Error: display() has private access in PrivateClass
        // pc.display(); 

    }

}

In this example, number and display are private, so they cannot be accessed directly from outside the PrivateClass. Access is provided through public methods.

Summary #

  • public: Accessible from any other class or package.
  • protected: Accessible within the same package and by subclasses.
  • Default (no modifier): Accessible only within the same package.
  • private: Accessible only within the same class.

Access modifiers help control the visibility of classes, methods, and variables, promoting encapsulation and protecting the internal state of objects.

Java
What are your Feelings

Share This Article :

  • Facebook
  • X
  • LinkedIn
What is final keyword? What is Encapsulation? 
Table of Contents
  • 1. Public
  • 2. Protected
  • 3. Default (No Modifier)
  • 4. Private
  • Summary
© 2025 V-Tech Solutions Ltd (UK), Reg. No: 16489105