DEV Community

Cover image for Access Modifiers in Java Explained for Beginners
Vinayagam
Vinayagam

Posted on

Access Modifiers in Java Explained for Beginners

Introduction

In Java, access modifiers are used to control how variables and methods can be accessed from different places. They are very important for writing safe and organized programs.

There are four types of access modifiers in Java:

  • public
  • protected
  • default
  • private

In this blog, we will understand these using a simple example with packages and inheritance.


What is an Access Modifier

An access modifier decides where a class, method, or variable can be used.

It answers questions like:

  • Can it be used inside the same class?
  • Can it be used in another class?
  • Can it be used in another package?

Types of Access Modifiers

1. Public

Public members can be accessed from anywhere in the program.

Example:

public int atmPin = 1234;
Enter fullscreen mode Exit fullscreen mode

You can use this variable in any class and any package.


2. Protected

Protected members can be accessed:

  • Inside the same package
  • In child classes from different packages

Example:

protected void makeBiriyani() {
    System.out.println("biriyani");
}
Enter fullscreen mode Exit fullscreen mode

3. Default

Default means no access modifier is given.

It can be accessed only inside the same package.

Example:

void show() {
    System.out.println("default method");
}
Enter fullscreen mode Exit fullscreen mode

4. Private

Private members can be accessed only inside the same class.

Example:

private int password = 123;
Enter fullscreen mode Exit fullscreen mode

Example Using Packages

We created two packages:

salem
madurai

This helps us understand how access works between different packages.


Parent Class

package salem;

public class Parent {

    public int atmPin = 1234;

    protected void makeBiriyani() {
        System.out.println("biriyani");
    }

    protected void checkBalance() {
        System.out.println("15000");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this class:

  • atmPin is public, so it can be accessed anywhere
  • methods are protected, so they follow special rules

Child Class (Same Package)

package salem;

public class Child extends Parent {

    public static void main(String[] args) {
        Child obj = new Child();
        obj.makeBiriyani();
        obj.checkBalance();
        System.out.println(obj.atmPin);
    }
}
Enter fullscreen mode Exit fullscreen mode

This class is in the same package as Parent.

So:

  • It can access protected methods directly
  • It can access public variables

Child2 Class (Different Package)

package madurai;

import salem.Parent;

public class Child2 extends Parent {

    public static void main(String[] args) {
        Child2 obj = new Child2();
        obj.makeBiriyani();
        obj.checkBalance();
        System.out.println(obj.atmPin);
    }
}
Enter fullscreen mode Exit fullscreen mode

This class is in a different package.

Here:

  • It extends Parent class
  • So it can use protected methods through inheritance

Access Behavior Based on Location

Let us understand how access changes based on location:

  • Same class → all modifiers work
  • Same package → public, protected, default work
  • Different package (child class) → public and protected work
  • Different package (non-child class) → only public works

Top comments (0)