Access Modifiers in Java: The Ultimate Gatekeepers

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?

Imagine you’re at a exclusive club, and you need to get past the bouncer to get in. The bouncer checks your ID, and if you’re on the list, you’re in! Access modifiers are like the bouncers of Java – they control who can access your classes, methods, and variables.

Types of Access Modifiers in Java

Java has four types of access modifiers: publicprivateprotected, 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!");
    }
}

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);
    }
}

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.

public class MyClass {
    protected int myVariable;

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

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

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 neighbourhood 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();
    }
}

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.


Conclusion

Access modifiers are the gatekeepers of Java, controlling who can access your classes, methods, and variables. By using the right access modifier, you can ensure that your code is secure, maintainable, and scalable.

So, go ahead and start using access modifiers in your Java code. Your code (and your data) will thank you!. Why I am sharing this because in upcoming articles we are gone use heavily in code. Inspired by Prakruti Vyas.

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply