Types of Access Modifiers in Java

Types of Access Modifiers in Java

In Java, access modifiers are keywords used to set the accessibility or visibility of classes, methods, variables, and constructors. They determine which other classes can access a particular class, method, or variable. There are four main access modifiers in Java:

Types of Access Modifiers in Java

Types of Access Modifiers

  • 1. Public
  • 2. Protected
  • 3. Default (Package-Private)
  • 4. Private

1. Public

  • Scope: The widest scope. A public class, method, or variable can be accessed from any other class, regardless of the package.
  • Syntax: `public`.

Usage
  ```java
  public class MyClass {
      public int myVariable;

      public void myMethod() {
          // method body
      }
  }
  ```

2. Protected

  • Scope: Accessible within the same package and by subclasses in other packages.
  • Syntax: `protected`

Usage
  ```java
  public class MyClass {
      protected int myVariable;

      protected void myMethod() {
          // method body
      }
  }
  ```

3. Default (Package-Private)

  • Scope: Accessible only within the same package. No keyword is used for default access.
  • Syntax: No explicit modifier needed; it is the absence of a modifier.

Usage
  ```java
  class MyClass { // default access
      int myVariable; // default access

      void myMethod() { // default access
          // method body
      }
  }
  ```

4. Private

  • Scope: The narrowest scope. A private method or variable can be accessed only within the same class.
  • Syntax: `private`

Usage
  ```java
  public class MyClass {
      private int myVariable;

      private void myMethod() {
          // method body
      }
  }
  ```

Demonstrating Access Modifiers

Example
```java
// File: com/example/MyClass.java
package com.example;

public class MyClass {
    public int publicVar = 1;
    protected int protectedVar = 2;
    int defaultVar = 3; // package-private
    private int privateVar = 4;

    public void publicMethod() {
        System.out.println("Public Method");
    }

    protected void protectedMethod() {
        System.out.println("Protected Method");
    }

    void defaultMethod() {
        System.out.println("Default Method");
    }

    private void privateMethod() {
        System.out.println("Private Method");
    }
}

// File: com/example/AnotherClass.java
package com.example;

public class AnotherClass {
    public void accessTest() {
        MyClass myClass = new MyClass();
        
        // Accessing variables
        System.out.println(myClass.publicVar); // accessible
        System.out.println(myClass.protectedVar); // accessible
        System.out.println(myClass.defaultVar); // accessible
        // System.out.println(myClass.privateVar); // not accessible, compilation error

        // Accessing methods
        myClass.publicMethod(); // accessible
        myClass.protectedMethod(); // accessible
        myClass.defaultMethod(); // accessible
        // myClass.privateMethod(); // not accessible, compilation error
    }
}

// File: com/other/SubClass.java
package com.other;

import com.example.MyClass;

public class SubClass extends MyClass {
    public void accessTest() {
        SubClass subClass = new SubClass();
        
        // Accessing variables
        System.out.println(subClass.publicVar); // accessible
        System.out.println(subClass.protectedVar); // accessible
        // System.out.println(subClass.defaultVar); // not accessible, compilation error
        // System.out.println(subClass.privateVar); // not accessible, compilation error

        // Accessing methods
        subClass.publicMethod(); // accessible
        subClass.protectedMethod(); // accessible
        // subClass.defaultMethod(); // not accessible, compilation error
        // subClass.privateMethod(); // not accessible, compilation error
    }
}
```

Summary

Access modifiers in Java control the visibility of classes, methods, and variables. The four main access modifiers are:

  • `public`: Accessible from anywhere.
  • `protected`: Accessible within the same package and by subclasses in other packages.
  • Default (no modifier): Accessible only within the same package.
  • `private`: Accessible only within the same class.

Using these access modifiers appropriately ensures encapsulation and proper access control within your Java programs.

Homepage

Readmore