Last Updated: 14 November, 2023
In Java, there are two types of modifiers: access modifiers and non-access modifiers. In the tutorial, we will learn about Java access modifiers, their types, and their uses in programs.
Access modifiers are used to control the accessibility or visibility of variables, methods, constructors, classes, and interfaces within a program.
Access modifiers play a significant role in object-oriented programming (OOP) by enforcing encapsulation and data hiding, which are vital concepts for maintaining code security and organization.
There are four types of access modifiers in Java:
Now, let's understand the uses of each access modifier one by one with the given examples.
The private access modifier restricts access to variables, methods, constructors, or nested classes within the class itself. Not even subclasses or other classes in the same package can access it from outside the class.
If we try to access private members outside the class, we will get a compilation time error, as given in the example below.
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method taste() from the type Apple is not visible at com.javabytechie.second.Fruit.main(Fruit.java:19)
There are two main ways to access private members from outside of a class:
1. Using getter and setter methods
As private members of a class cannot access outside directly but using the class's getter and setter methods, we can access or modify them from outside the class.
The getter and setter methods are the preferred approaches as compared to reflection. This approach provides a controlled and well-defined way to interact with the class's internal state.
Output
Fruit's Name: Apple
Fruit's Price: 200.5
In this example, we have two private variables named fruit_name and price. We are trying to access these private variables from the other class named Fruit.
Here, we have used the getter and setter methods to access the private variables.
👉 Getter methods are known as accessor methods and Setter methods are known as mutator methods in Java.
2. Using reflection
Reflection is one of the strong features of Java that allows us to access and modify the internal structure of a class at runtime. This includes accessing private fields and methods, as well as overriding methods and creating new instances of classes.
Reflection, however, must be used carefully since it has the ability to break encapsulation and produce unexpected behavior.
Output
Price: 250.6
Method Name: showPrice
Access Modifier: private
In this example, we have a private variable named price and a private method named showPrice(). Here, we are using the reflection to access the private variable and method of the class Apple.
If we don't specify any access modifiers, then that is the default modifier. The access level of a default modifier is only within the package. It cannot be accessed from outside the package.
Output
Fruit tastes sweet.
default is a keyword in Java that is used with interface methods and switch statements. For the default access modifier, we don't have to specify any keywords.
The access level of a protected modifier is within the package and outside the package through the child class. It cannot be accessed from classes in different packages unless they are subclasses of the class containing the protected member.
Output
Apple taste is sweet.
Here, the Apple class has a protected method that is being accessed outside the package because Apple is a child class of Fruit, so using the object of Fruit class, the protected method taste() is being called.
Note: If we try to access the default method outside the package directly, we will get a compile-time error.
The public access modifier allows unrestricted access to variables, methods, constructors, or nested classes from anywhere within the program. public access modifier is a global access modifier within the project. It can be accessed from within the class, outside the class, within the package, and outside the package.
Output
Apple price is 200 Rupees.
Apple taste is sweet.
Here, Apple is a public class, which means this class can be accessed from any different package, and this class has two public-type static and instance methods. The Apple class and its methods are being accessed in another class of a different package.
Here's a table summarizing the access levels and visibility of different access modifiers:
Access Modifier | within class | within package | outside package by subclass only | outside package |
---|---|---|---|---|
Private | ||||
Default | ||||
Protected | ||||
Public |
Access modifiers are an important part of Java because they provide an approach for managing access to various parts of a program. They help in the maintenance of code security, the prevention of unwanted access to sensitive data, and the promotion of modularity and organization. Developers can guarantee that their code is well-structured, manageable, and safe by carefully selecting the proper access modifiers.
👉 Java also supports many non-access modifiers, such as static, abstract, synchronized, native, volatile, transient, etc.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com