Sunday, July 18, 2021

Access Modifiers in Java - Public, Private, Protected, and Package Examples

public, private, protected and package or default are four access modifiers available in Java. These access modifiers provide Java programmers to control the accessibility or visibility of a class, method, or any field of a class. A good understanding of public, private, or protected modifiers is required in order to implement proper encapsulation in Java and create a Java program that is easier to maintain. In this Java tutorial, we will see what is public, private, protected and default modifiers are, which modifiers can be used with top-level class and nested class, and what is the difference between public, private, protected, and default modifiers in Java.


In the next section, we will discuss all four access modifiers in decreasing order of accessibility, starting from the public which is most accessible, and going down to private which is least accessible.



Public vs Protected vs Package vs Private modifier in Java

In this section, we will see each of these access modifiers and learn about them. Starting from the public, which is least restrictive to private which is most restrictive and provides the highest form of encapsulation in Java.

Access Modifiers in Java - Public, Private, Protected, and Package Examples




1. The public modifier in Java

The public is the most common and least restrictive access modifier in Java. You can apply public modifiers into variables, methods, and both top-level and inner classes in Java. Since the public modifier offers the least amount of encapsulation it's difficult to change, any publicly exposed API is hard to change without breaking its client. 

So always put extra care while designing public API means making your class, method, or variable public in Java. It's not good practice as well to make fields of a class public because it goes against encapsulation and exposing the internals of class removes any chance of improving internal details with better performance alternatives. 

That’s why it's better to prefer the getter and setter method over public fields. Think twice before making variables public in Java. So despite being the most popular access modifier in a test environment, the public should be used carefully in production code. On the syntactical front, all interface methods are by default public in Java.

Here is an example of a public modifier in Java:

public class HelloWorld{
  public final static int count = 1;
 
  public int getCount(){
   return count;
 }
}

In this example, class HelloWorld, variable count, and method getCount() are all public which means they are accessible from anywhere.  You can further see the Java programing courses to learn more about essential Java concepts. 

2. The protected modifier in Java

The protected access modifier is similar to the public modifier and can be applied to variables, methods, and nested classes in Java. Though it has two notable differences with a public modifier. public variables are accessible to everyone but the protected variable is only accessed inside subclass outside of the package it has declared. 

The second difference between public and protected modifiers is that, unlike public modifiers, a protected modifier can not be applied to a top-level class, Though you can still apply it to nested or inner classes in Java. a protected method can be overridden by any public or protected method and only data that is supposed to be different for sub-class is made protected. 

Though protected offers better encapsulation than public keywords it's still not the best form of encapsulation available and should be avoided as much as possible. If you have a choice of making a variable, class, or method protected or the public, choose protected.

Here is an example of a protected access modifier in Java:

public class HelloWorld{
  public final static int count = 1;
 
  protected int getCount(){
   return count;
 }
}

In this example, method getCount() is protected which means they are accessible to any class in the same package but only accessible to the subclass of HelloWorld in outside the package.  


3. Package or default access modifier

package level access is the default access level available to any Java class, method, or variable. If you don't specify any access modifier during variable or method declaration, the Java compiler will provide them package level access. 

It is also referred to as package-private as any variable which is package-private is only accessible inside that package. The difference between protected and package level access is that It more restrictive than the protected modifier and it's a default access level provided by Java. 

The package-level API is easier to change than public API as it is most likely to be developed by the same person who is developing other classes in a package and you are sure that no client other than the same package is using it. If you have a very good reason for modifying the internals of package-level class you can do that with minimal changes, as all changes will be confined up to package level only. 

Another difference between protected and package modifiers is that package modifiers can be used on the top-level class as well.

Here is an example of a private access modifier in Java:

class HelloWorld{
  final static int count = 1;
 
  int getCount(){
   return count;
 }
}

In this example, class HelloWorld, variable count, and method getCount() are all have package level access which means they are accessible only inside the access. They are not visible outside the package. 


4. Private access modifier

private access modifier is the most restrictive access modifier in Java and its Java best practice is to make variables, methods private by default. I have discussed why should you make variables private in Java but the major reason for this is solid encapsulation provided by private keywords. private code is easier to change and replace by a better performance version.

It's good policy to declare an interface as public and keeping implementation private in Java. Another important point about private access modifiers is that unlike public they are not accessible anywhere other than the class they are declared. You can not apply private modifiers on top-level classes and the private method can not be overridden

Another interesting use of the private modifier is making the constructor private to not allow an external class to create instances and instead, force them to use static factory methods

One of the popular examples of a private constructor is the Singleton pattern in Java, which makes it contractor private and instead provides a getInstance() method which controls a number of instances.

Here is an example of a private modifier in Java:

public class HelloWorld{
  private int count = 1;
 
  public int getCount(){
   return count;
 }
}

In this example, the variable count is private which means they are accessible only within the class. That's why we have declared a method called getCount() for all the clients who want this count from outside this class. This is a general pattern of accessing variables in Java. 


That's all on What is public, protected, default (package-private), and private access modifier in Java and the difference between public, private, protected, and package modifier. Access modifier is one of the most important fundamentals for any Java programmer and clever usage of access modifier is required to build flexible and highly maintainable Java applications. In short use public, protect, default, and private modifiers carefully and if you are in confusion make variables private.


Other Java programming tutorials from Java67 Blog

1 comment:

  1. Hi I have doubt why we can not access protected inner class outside of package

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.