Introduction


Access modifiers and non-access modifiers are the two types of modifiers in Java. Non-access modifiers explain the behavior of the entities to the JVM. Whereas access modifiers in Java let you control the accessibility of classes, interfaces, and variables. One can also control the visibility of methods, constructors, data members, and setter methods with access modifiers. Therefore, non-access modifiers describe an entity's behavior, and access modifiers define a program's visibility or scope.


This blog will show you what access modifiers are, their types, and examples to help you understand better. So let’s start with the definition.


Access Modifiers Definition and Meaning


Access modifiers, also known as access specifiers, are keywords in programming languages. It determines the visibility and accessibility of classes, methods, variables, and other members within a program. They control which parts of a program can be accessed or modified from other parts of the code.


Common access modifiers in programming languages like Java, C++, and C#.


Types of Access Modifier in Java


Below are the four types of access modifiers available in Java: 


1. Default 


No keyword is required here. When you don't specify Java members like class or methods or data members with any access modifier, they fall into the category of default access modifiers by default. You can access these methods or data members within the same package. Thus, you cannot access them from outside the package. 


Default access modifiers in Java offer better visibility than private access modifiers. However, it has more limitations than protected and public access modifiers.


2. Public


This modifier is a keyword. Any class member, such as a variable, method, or data member, can be accessed from anywhere in the program if it is prefixed with the public access modifier. Thus, it allows access from both inside and outside of the same class. 


Furthermore, you can access it within the same package and also from outside the package. You can also globally access members like variables, methods, etc.  Public access modifiers in Java help with the access to the members. Also, there are no limitations on public access modifier members. So, in contrast to the other access modifiers, it has more accessibility or visible scope.


3. Protected


This access modifier type is a keyword. A protected modifier allows inheritance to give access to a class's methods or data members. It provides access both inside and outside the package. But, when you compare it to private and default access modifiers, it is more accessible. However, it is not as noticeable as the public access modification.


4. Private


You specify private Java modifiers when any member of a class is prefixed with the private keyword. This is also the most restricted access modifier in contrast to other modifiers. Prefixing the methods or data members with a private access modifier is helpful. It can also limit the visibility of these methods and data members. Thus, you can access them only within the same class where they are declared. But, they will not be visible to the outside world. These were the four types of access modifiers in Java along with their applicability. 


Example of Access Modifiers in Java


Below are a few examples of access modifiers :
 

1. // Public access Java modifier

public class PublicClass {

public int publicField;

public void publicMethod() {

// code here

}

}


2. // Protected access Java modifier

protected class ProtectedClass {

protected int protectedField;

protected void protectedMethod() {

// code here

}

}


3. // Default access Java modifier

class DefaultClass {

int defaultField;

void defaultMethod() {

// code here

}

}


4. // Private access Java modifier


class PrivateClass {

private int privateField;

private void privateMethod() {

// code here

}

}


Why are Access Modifiers important?


Modifiers in Java define which classes can access particular methods, variables, or other classes. You may restrict access to different classes, methods, constructors, and variables using access specifiers. It also guarantees the encapsulation and reuse of Java entities.


Algorithm to use an access modifier 


Below is a simple algorithm for using access modifiers in Java:
 

  • Define a class by creating a class to represent the object you need to manage. Now define instance variables within the class that represent the data you need to manage.
  • Specify an access modifier for each instance variable. These Java access modifiers must also determine the visibility of the variable.
  • Use the private access modifier to restrict access to a variable from within another class. This access modifier also offers the highest level of encapsulation and is the most restricted.
  • Use the protected access modifier to access a variable from within the class and its subclasses. This allows for an amount of inheritance and is less restricted than private.
  • Use the public access modifier to give universal access to a variable. These access modifiers in Java offer the smallest level of encapsulation. Additionally, they are also the least restrictive.


You can manage access to the variables through accessor and mutator methods. Many times, the variables have a public access modifier, accessor (getter), and mutator (setter) methods. But, these techniques are still helpful in such cases. They also help to access and alter the variables. This adds an abstraction level and improves the testability and maintainability of your code.


Conclusion


An access modifier in Java is a keyword in object-oriented programming languages. It helps define the scope and visibility of a program's classes, methods, fields, and constructors. Access modifiers also control how you can access these elements from various program parts, like other classes or packages. For more technical expertise, join The IoT Academy and be a master of in-demand programming languages.


Frequently Asked Questions


Q.What are the 12 modifiers in Java?

Ans.12 Java Modifiers include public, private, native, strictfp, protected, default, final, synchronized, abstract, transient, and volatile.


Q.What are modifier rules?

Ans.There are some standard Java modifier rules. You cannot make Classes and interfaces private. But, you can make the nested classes private. Moreover, setting the access modifier of getters methods is not possible.