What is public private protected and package or default access modifier in Java

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

1. The public modifier in Java

The public is the most common and least restrictive access modifier in Java. You can apply public modifier into variables, methods and both top-level and inner classes in Java. Since public modifier offers least amount of encapsulation it's difficult to change, any publicly exposed API are 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 alternative. 

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

2. The protected modifier in Java

What is private, protected, package and public modifier in Java protected access modifier is similar to 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 package it has declared. 

The second difference between public and protected modifier is that unlike public modifier, a protected modifier can not be applied to a top-level class, Though you can still apply it to nested or inner class 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 keyword 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.

3. Package or default access modifier

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

It is also referred 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 protected modifier and its a default access level provided by Java. 

The package level API are easier to change than public API as it is most likely 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 are using it. If you have a very good reason of 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 modifier that package modifier can be used on the top level class as well.

4. Private access modifier

private access modifier is most restrictive access modifier in Java and its Java best practice to make variables, methods private by default. I have discussed this on why should you make variables private in Java but the major reason of this is solid encapsulation provided by private keyword. private code is easier to change and replace by 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 modifier is that unlike public they are not accessible anywhere other than the class they are declared. You can not apply private modifier on top level class and private method can not be overridden

Another interesting use of the private modifier is making 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 Singleton pattern in Java , which makes it contractor private and instead provides a getInstance() method which controls a number of instances.

That's all on What is public, protected, default (package-private) and private access modifier in Java and 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 application. In short use public, protect, default and private modifier carefully and if you are in confusion make variables private.

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Complete Java Masterclass

Other Java programming tutorials from Java67 Blog

No comments:

Post a Comment

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