Access Modifiers in Java: Controlling Visibility and Accessibility

Abu Talha
3 min readOct 8, 2023

--

Access modifiers in Java are a fundamental concept that governs the visibility and accessibility of classes, methods, fields, and other members within a Java program. They play a crucial role in encapsulation, security, and maintaining the integrity of Java code. In this article, we will delve into the various access modifiers in Java and provide professional examples to illustrate their usage.

Why Are Access Modifiers Important?

Access modifiers define the scope of a class, method, or field, determining whether it can be accessed from other parts of the program or from external code. They help encapsulate the internal implementation details of a class while exposing only the necessary and safe interfaces to the outside world. Access modifiers enhance code readability, maintainability, and security.

Types of Access Modifiers in Java

Java offers four main types of access modifiers:

Public (public): The most permissive access level. Members declared as public are accessible from any other class or package.

Protected (protected): Members declared as protected are accessible within the same class, subclasses (even if they are in different packages), and within the same package.

Default (no modifier): Members with no explicit access modifier (package-private) are accessible within the same package only. This is often referred to as “default” or “package-private” access.

Private (private): The most restrictive access level. Members declared as private are accessible only within the same class.

Examples of Access Modifiers

Public Access Modifier

public class PublicExample {
public String publicField = "This is a public field.";

public void publicMethod() {
System.out.println("This is a public method.");
}
}

In this example, the publicField and publicMethod() are accessible from anywhere, even from outside the class.

Protected Access Modifier

public class Parent {
protected int protectedField = 42;

protected void protectedMethod() {
System.out.println("This is a protected method.");
}
}

public class Child extends Parent {
public void accessProtectedMembers() {
System.out.println("Accessing protectedField: " + protectedField);
protectedMethod();
}
}

Here, protectedField and protectedMethod() in the Parent class are accessible within the Child the class due to the protected access modifier.

Default (Package-Private) Access Modifier

class PackagePrivateExample {
String defaultField = "This is a default field.";

void defaultMethod() {
System.out.println("This is a default method.");
}
}

Members in classes with no explicit access modifier (like PackagePrivateExample) are accessible only within the same package.

Private Access Modifier

public class PrivateExample {
private String privateField = "This is a private field.";

private void privateMethod() {
System.out.println("This is a private method.");
}
}

Members declared as private are accessible only within the same class, as shown in the PrivateExample class.

Access modifiers in Java are essential for controlling access to classes, methods, and fields, contributing to code encapsulation, maintainability, and security. By carefully choosing the appropriate access level for your members, you can strike a balance between accessibility and encapsulation, ensuring that your Java code remains robust and well-structured.

Thank You !

For more updates like & follow!

--

--

Abu Talha
Abu Talha

Written by Abu Talha

SQA Engineer | Security Researcher | Application Penetration Tester | Back-End Developer

No responses yet