Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java For Automation

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java For Automation
  • 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:

// 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:

// 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:

// 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:

// 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
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105