17 October 2020

What is access modifier in Java?

Welcome to another tutorial on the "How to code in Java" tutorial series. In the last tutorial, we have gone through on how to use a constructor to initialize an object. In this tutorial, we will be looking at how Access Modifier works and how to use them correctly.


We can use an access modifier on class's attributes, methods (including constructor), as well as the class itself. We use access modifier to provide different visibility or accessibility for the components mentioned. There are four different types of access modifier:


Four different types of access modifier

Level restriction for each access modifier.


Index for the Tutorial:


Default access modifier.


When we did not specify an access modifier, the compiler will treat the component to have a default access modifier by default. By having this type of modifier, the component is accessible only to classes that are in the same package(similar to a folder).


Accessing a class method with default access modifier.


The output of the code.


However, if we try to access a component with default access in another package, we would get a compile-time error after we compile the code. If you're using an IDE, then the IDE would give you an alert about the mistake (That is why IDE is so useful). 


Creating a class and a method with both having default access modifier.

As the code is shown, we cannot access component with default access modifier from another package.


Error after we compile the code with the error.


You can use the default access modifier when the component is only needed in one single package.


Public access modifier.


Components that have a public access modifier are visible/accessible from anywhere, from different classes, same class, same package and from a separate package. It has no restriction at all! 


Changing the class and its method to have a public access modifier.


The class and method could be accessed from another class in the same package.


The class and method could also be accessed from another class from a different package.


However, it would be smart if you were careful with how you specify the access modifier for methods and attributes/fields. Because let's say you specify a public access modifier for a class, but you defined a method inside it with a default access modifier. The method will not be accessible or visible to other classes from a different package. 


Changing the class's method to have a protected access modifier.


The method could not be found, although the class can be found.


The case would be the same when you provide the method with the others access modifier. That's why by giving a class a specific access modifier doesn't mean that the fields and methods inside it will also have the same access modifier. Although the class is visible to other classes anywhere else, the methods and fields of the class will also need to have their appropriate access modifier.


You can consider giving a component to having a public access modifier if you need to use the component in many different places that might span into different packages. But also make sure to specify the class of the component to have the right access modifier so that other class that needs the component could find its class. 



Protected access modifier.


Protected access modifier is a bit special compared to the other modifier.  We cannot apply a protected modifier to interface and class, only applicable for attributes, methods and constructor. The methods and fields inside an interface also cannot have a protected access modifier. 


A protected access modifier makes the component visible/accessible within the same package and outside of the package. But the component can only be accessible by only through inheritance if we access it from a different package from the component. Another thing is we will also need to import the class into the class which is in a different package if we want to inherit that class(because they're in different packages). Inheritance would be explained in a future tutorial, for now, know that we can only access a protected component through inheritance if we access it from another package from the component.


The FirstClass maintain the same code as the previous example.

Methods with protected access modifier could be accessed by other class within the same package.


To access the method from another package's class, we'll need to inherit the class into the caller class.


If your component (fields, methods, etc.) needs to be used in other packages but needed some constraints in using it(constraining the data changing), consider giving it a protected access modifier will be a great choice.



Private access modifier.


Components with private access modifier are only accessible within the same class. The class would not be visible/accessible from other class in the same or different file and classes from another package. 


We also cannot apply a private access modifier to a class and an interface, only applicable to methods, fields/attributes, and constructor. Methods, fields/attributes and constructor in an interface are also can't have a private access modifier. But, that's said, a nested class can be set to have a private access modifier(I won't be covering it in this series but I might make a tutorial for it).


Changing the "printMessage()" method to have private access modifier.



As you can see, here the class is in the same package as the "FirstClass" class. The class can access "FirstClass" but could not find the "printMessage()" method. That is because the method has a private access modifier. 


The method and class cannot be accessed from another class which is in another package.


The reason why the compiler could not find the "FirstClass" in "SecondClass" is that the class has a default access modifier which restricted to only be accessible by classes that's in the same package. While as the method "printMessage()" has a private access modifier, the method is not accessible from any other places besides in its class itself.


So you might be asking how do we even access the method? It's simple, just use another method with a less restrictive access modifier to call that method. This technique is often used in encapsulation, although my example didn't really implement the concept itself. Encapsulation will be discussed later on in a future tutorial, so don't worry much. 


This would output the text "From firstPackage".


You can consider applying a private access modifier to a component if that component should be only accessible within the same class (hiding it). More info on utilizing private access modifier will be discussed in encapsulation.



That's about all the things that I could take about on access modifier. I hope you now know how to use the different types of access modifier and when to use the correct one. If you find this tutorial helpful, please share it with others that might need it or are interested in it. Also if you have any critics, suggestion, questions, feel free to leave a comment below. In the next tutorial, we'll be looking at constant fields, static fields and methods, and much more. So stay tuned.

No comments:

Post a Comment