Explain Access Modifiers in Java with example

Access Modifiers in Java

Access Modifiers in Java, access modifiers determine the visibility and accessibility of classes, methods, and variables. There are four main types of access modifiers:

  • Public: The code is accessible from any other class.
  • Private: The code is accessible only within the declared class. By default, the member of a class are private. Such a class is completely hidden from the outside world and does not serve any purpose.
  • Protected: The code is accessible within the same package and by subclasses.
  • Default (no modifier): The code is accessible only within the same package.

Public Access Modifiers in Java

Example

// File: PublicClass.java
public class PublicClass {
    public void display() {
        System.out.println("This is a public method.");
    }
}

// File: TestPublic.java
public class TestPublic {
    public static void main(String[] args) {
        PublicClass obj = new PublicClass();
        obj.display(); // Accessible from any class
    }
}

Private Access Modifiers in Java

Example

// File: PrivateClass.java
public class PrivateClass {
    private void display() {
        System.out.println("This is a private method.");
    }

    public void accessPrivateMethod() {
        display(); // Accessible within the same class
    }
}

// File: TestPrivate.java
public class TestPrivate {
    public static void main(String[] args) {
        PrivateClass obj = new PrivateClass();
        // obj.display(); // Not accessible, will cause a compile-time error
        obj.accessPrivateMethod(); // Accessible through a public method
    }
}

Protected Access Modifiers in Java

Example

// File: ProtectedClass.java
public class ProtectedClass {
    protected void display() {
        System.out.println("This is a protected method.");
    }
}

// File: SubClass.java
public class SubClass extends ProtectedClass {
    public void callDisplay() {
        display(); // Accessible in subclass
    }
}

// File: TestProtected.java
public class TestProtected {
    public static void main(String[] args) {
        SubClass obj = new SubClass();
        obj.callDisplay(); // Accessible through subclass method
        // obj.display(); // Not directly accessible outside the package
    }
}

Default (Package-Private)

// File: DefaultClass.java
class DefaultClass {
    void display() {
        System.out.println("This is a default (package-private) method.");
    }
}

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

Summary

  • Public: Accessible from anywhere.
  • Private: Accessible only within the same class.
  • Protected: Accessible within the same package and subclasses.
  • Default: Accessible only within the same package.

These access modifiers help encapsulate the code and define how it can be interacted with from other parts of the program.

Comments

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

Leave a Reply

Your email address will not be published. Required fields are marked *