DEV Community

Arun Kumar
Arun Kumar

Posted on

Access Modifiers in Java: The Security propose

Hey there, I am back with another exciting java topic! Are you ready to take your coding skills to the next level? Today, we’re going to explore one of the most fundamental concepts in Java access modifiers!

What's an Access Modifier, Anyway?

In Java, access modifiers are keywords used to define the visibility of classes, methods, constructors, and variables. They one of the core principles of object-oriented programming (OOP).

Types of Access Modifiers in Java

Java has four types of access modifiers: public, private, protected, and default (also known as "package-private"). Each modifier has its own set of rules, so let's dive in and explore each one!

1. public Access Modifier

The public access modifier is like the VIP pass of Java – anyone can access your classes, methods, and variables from anywhere.

public class MyClass {
    public void myMethod() {
        System.out.println("Hello, World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the MyClass class and its myMethod() method are accessible from anywhere.

2. private Access Modifier

The private access modifier is like the secret password of Java – only the class itself can access its own private members.

public class MyClass {
    private int myVariable;

    public void myMethod() {
        myVariable = 10;
        System.out.println(myVariable);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the myVariable variable is private, and only the MyClass class itself can access it.

3. protected Access Modifier

The protected access modifier is like the family secret of Java – only the class itself and its subclasses can access its protected members. Used with import the package and inherit the class file to use it.

public class MyClass {
    protected int myVariable;

    public void myMethod() {
        myVariable = 10;
        System.out.println(myVariable);
    }
}
Enter fullscreen mode Exit fullscreen mode

public class MySubClass extends MyClass {
    public void mySubMethod() {
        myVariable = 20;
        System.out.println(myVariable);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the myVariable variable is protected, and both the MyClass class and its subclass MySubClass can access it.

4. default (or "package-private") Access Modifier

The default access modifier is like the neighborhood secret of Java – only classes within the same package can access its default members.


// MyClass.java
package com.example.mypackage;

public class MyClass {
    int myVariable;

    public void myMethod() {
        myVariable = 10;
        System.out.println(myVariable);
    }
}

// MyOtherClass.java
package com.example.mypackage;

public class MyOtherClass {
    public void myOtherMethod() {
        MyClass myClass = new MyClass();
        myClass.myMethod();
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the myVariable variable is default (or package-private), and both the MyClass class and the MyOtherClass class within the same package can access it.

Top comments (0)