Java Access Modifiers


Introduction

In Java, access modifiers are keywords that set the accessibility of classes, methods, and other members. They control where the members can be accessed from and ensure encapsulation. Java provides four main access modifiers: public, private, protected, and default (no modifier).

Table of Contents

  1. What are Access Modifiers?
  2. Types of Access Modifiers in Java
    • Public
    • Private
    • Protected
    • Default
  3. Examples of Each Access Modifier
  4. Conclusion

What are Access Modifiers?

Access modifiers in Java define the scope of accessibility of a class, method, constructor, or variable. They help in implementing the principle of encapsulation by restricting access to certain members from outside the class or package.

Types of Access Modifiers in Java

Java provides four main types of access modifiers:

  1. Public
  2. Private
  3. Protected
  4. Default (no modifier)

1. Public

The public access modifier makes a class, method, or variable accessible from any other class. There are no restrictions on its visibility.

Example:

public class PublicExample {
    public int data = 10;

    public void display() {
        System.out.println("Public method");
    }

    public static void main(String[] args) {
        PublicExample obj = new PublicExample();
        System.out.println(obj.data); // Accessible
        obj.display(); // Accessible
    }
}

2. Private

The private access modifier restricts the access of a class, method, or variable to within the same class only. It is not accessible from any other class.

Example:

public class PrivateExample {
    private int data = 10;

    private void display() {
        System.out.println("Private method");
    }

    public static void main(String[] args) {
        PrivateExample obj = new PrivateExample();
        System.out.println(obj.data); // Accessible
        obj.display(); // Accessible
    }
}

3. Protected

The protected access modifier allows access to the members of a class within the same package and by subclasses in different packages.

Example:

public class ProtectedExample {
    protected int data = 10;

    protected void display() {
        System.out.println("Protected method");
    }
}

class Subclass extends ProtectedExample {
    public static void main(String[] args) {
        Subclass obj = new Subclass();
        System.out.println(obj.data); // Accessible
        obj.display(); // Accessible
    }
}

4. Default (No Modifier)

When no access modifier is specified, it is known as the default access modifier. It allows access to the members of a class within the same package only.

Example:

class DefaultExample {
    int data = 10; // default access modifier

    void display() {
        System.out.println("Default method");
    }

    public static void main(String[] args) {
        DefaultExample obj = new DefaultExample();
        System.out.println(obj.data); // Accessible
        obj.display(); // Accessible
    }
}

Examples of Each Access Modifier

Public

public class PublicClass {
    public int data = 100;

    public void showData() {
        System.out.println("Public Data: " + data);
    }
}

class TestPublic {
    public static void main(String[] args) {
        PublicClass obj = new PublicClass();
        obj.showData(); // Accessible
    }
}

Private

class PrivateClass {
    private int data = 200;

    private void showData() {
        System.out.println("Private Data: " + data);
    }

    public static void main(String[] args) {
        PrivateClass obj = new PrivateClass();
        obj.showData(); // Accessible within the same class
    }
}

Protected

class ProtectedClass {
    protected int data = 300;

    protected void showData() {
        System.out.println("Protected Data: " + data);
    }
}

class SubClassProtected extends ProtectedClass {
    public static void main(String[] args) {
        SubClassProtected obj = new SubClassProtected();
        obj.showData(); // Accessible in subclass
    }
}

Default

class DefaultClass {
    int data = 400;

    void showData() {
        System.out.println("Default Data: " + data);
    }
}

class TestDefault {
    public static void main(String[] args) {
        DefaultClass obj = new DefaultClass();
        obj.showData(); // Accessible within the same package
    }
}

Conclusion

Access modifiers in Java are crucial for encapsulation and defining the visibility of classes, methods, and variables. Understanding how to use public, private, protected, and default access modifiers allow developers to effectively control access to the components of their code, enhancing security and maintainability.


Comments