Saturday, December 29, 2012

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

public, private, protected and package or default are four access modifier available in Java. These access modifier provides Java programmer to control accessibility or visibility of class, method or any field of 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 are difference between public, private, protected and default modifier in Java. We In next section we will discuss all four access modifier in decreasing order of accessibility, starting from 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 modifier and learn about them. Starting from public, which is least restrictive to private which is most restrictive and provide highest form of encapsulation in Java.

Public modifier in Java
public is 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 its 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. Its not good practice as well to make fields of a class public because it goes against encapsulation and exposing internals of class removes any chance of improving internal details with better performance alternative. That’s why its better to prefer getter and setter method over public fields.Think twice before making variables public in Java. So despite of most popular access modifier in test environment, public should be used carefully in production code. On syntactical front all interface methods are by default public in Java.


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 difference with public modifier. public variables are accessible to everyone but protected variable is only access inside subclass outside of package it has declared. Second difference between public and protected modifier is that unlike public modifier, protected modifier can not be applied to top level class, Though you can still apply it to nested or inner class in Java. protected method can be overridden by any public or protected method and only data which is supposed to be different for sub-class is made protected. Though protected offers better encapsulation than public keyword its 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 public, choose protected.


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. Difference between protected and package level access is that, It more restrictive than protected modifier and its a default access level provided by Java. package level API are easier to change than public API as it is most likely be developed by same person who is developing other classes in package and you are sure that no client other than same package are using it. If you have very good reason of modifying 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 top level class as well.

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 major reason of this is solid encapsulation provided by private keyword. private code is easier to change and replace by better performance version.Its good policy to declare 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 private modifier is making constructor private to not allow external class to create instances and instead force them to use static factory methods. One of the popular example of private constructor is Singleton pattern in Java , which makes it contractor private and instead provide a getInstance() method which controls 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 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.

Other Java programming tutorials from Java67 Blog

No comments:

Post a Comment

Java67 Headline Animator