Friday, September 14, 2018

Difference between private, protected, public and package modifier or keyword in Java

Private vs Public vs Protected vs Package in Java


Java has four access modifiers: private, protected and public. Packet-level access is the default access level provided by Java if no access modifier is specified. These access modifiers are used to restrict the accessibility of a class, method or variable in which it is applied. Let's start with the private access modifier which is the most restrictive access modifier and then go to the public that is the least restrictive access modifier, along the way we will see some best practices while using the access modifier in Java and some examples of use of private and protected keywords.

Private keyword in Java

Keyword or private modifier in Java can be applied to the member, method or class nested in Java. You cannot use the private switch in the top-level class. The variables, methods and particular classes are accessible only in the class in which they are declared. Private is the highest form of Java encapsulation API and should be used as much as possible. It is best practice to encode in Java declare private variable by default. A private method can only call the class in which it was declared.



According to the rules of method substitution in Java, a private method cannot be replaced either. The private keyword can also be applied to the constructor and, if it becomes the private constructor, it will prevent it from being subclassified. A popular example of making the private constructor is the Singleton class in Java, which provides the getInstance () method to get the object instead of creating a new object using the Java constructor. Here are some differences between private and protected access, the public and the package level

Package or default access level in Java

There is no access modifier called package, instead, the package is a keyword that is used to declare a package in Java, a package is a directory in which a Java class belongs. The standard or packet access level is the second highest restricted access modifier after private and any variable, method or class declared as private-package is accessible only in the package to which it belongs. The good thing about the standard modifier is that the top-level class can also be private for the packet if there is no access switch at the class level.

Protected keyword in Java

The difference between the private and protected keyword is which protected method, variable or nested class is not only accessible within a class within the package, but also outside the package in a subclass. If you declare a protected variable, anyone can use it if you extend your class. The top-level class can not be protected as well.

Public keyword in Java

Public is the least restrictive access modifier in the Java programming language and its bad practice is to declare field, method or class by default public because, once made public, it is very difficult to make any change in the internal structure of the class, because affects all customers. this. Making the class or instance variable public also violated the encapsulation principle, which is not good at all and greatly affects maintenance. Instead of making variable public, you should make it private and provide public getter and setter. The public modifier can also be applied to a top-level class. In Java, the file name must be the same with the public class declared in the file.

This is all the difference between the private, protected, package and public access modifier. As you saw, the difference between private and public resides in how accessible a particular field, method or class would be. Public means that you can access it anywhere, while private means you can only access it within your own class.

Just to observe that all private, protected or public modifiers are not applied to local variables in Java, a local variable can only be final in java.











No comments:

Post a Comment

From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9 i...