Skip to main content

Access Modifiers in Java

 What are private,  package-private, protected, and public access modifiers in java?

Java provides four access modifiers and theses modifies are used to restrict the visibility of a class, variable, or a method. Also, these access modifiers determine that other classes can access a particular variable or call a particular method of the current class. Java has provided the two levels of access controls:
  1. At the top level (Top level classes/interfaces)
  2. At the member level (variables, methods, classes/interfaces inside the top-level classes/interfaces)
At the top-level, you can use only public and package-private (there is no explicit modifier for this). For example:

And at the member level, you can use any of the four access modifiers. For example:

Now let's move further in detail about all these four access modifiers. We will start from the public access modifier that is like a free bird I guess and move toward the private access modifier that is the most restricted one.

Public Access Modifier

  1. Public access modifier is like a free bird. It means it is the least restrictive modifier in Java. The variables or methods that define as the public can be accessed anywhere in the world. You can define top-level classes as public. 
  2. Making the variable public is a bad practice because in the future it is very difficult to make any change in the class as the public variable will be accessed by other classes directly. Instead of defining it public, you can define it private and provide getters and setters for accessing it from other classes.

Protected Access Modifier

  1. The variable, methods, constructors, or nested classes defined as protected can be accessed in the same package and outside the package in subclasses.
  2. You cannot define top-level classes as protected.

Package-private (no explicit modifier)

  1. For this modifier, there is no explicit access modifier provide by Java. If you do not declare variables, methods, constructors, or nested classes as public, protected, or private then it is by default package-private.
  2. As from the name it is very clear that the variables, methods, or nested classes declared without any modifier(public, protected, or private) can only be accessed inside the same package.
  3. You can define top-level classes with no modifier and that class will be package-private.

Private Access Modifier

  1. Defining variables as private and access them through getters and setters is a good coding practice.
  2. You can define variables, methods, constructors, or nested classes as private. Top-level classes cannot be defined as private.
  3. A class having a constructor private can not be subclassed that is you can extend it.

That's all about the access modifiers in java. Please do comment below if there is any query or feedback.

Comments

Popular posts from this blog

How to add an existing project to a GitHub Repository

  Add Existing project to a GitHub Repository Step 1: Login into your GitHub account and create a new repository. Step 2: Go to your project folder on your machine and open git bash. After that runs the command “ git init ”. This command initialized the empty git repository on your local machine. Step 3: Create a file “. gitignore ” using the command “ echo > .gitignore ”. Add the file extension or file name inside this file, those you don’t want to commit to your repository. Git will ignore all those files that will be present inside this file. Step 4: Add all the files to the staging area using the command “git add .” This command adds all the files to the staging area and all these files are now ready for the commit. And with the help of the command “git status” , you can check that all the files are inside the staging area. Also, you can see the file extension or the file name mentioned inside the “.gitignore”  are ignored by git. Step 5: Commit the files that are pre...

Lambda Expression

What is Lambda Expression? In java 8, a powerful concept is added named Lambda Expression.  Lambda expression is an anonymous function (a nameless function that does not have a modifier, return type, and even the type of its arguments).  Lambda expression is that you can pass to a method whose argument type is functional interface i.e. an interface that is either annotated by @FunctionalInterface  annotation e.g. interfaces from java.util.function package, or an interface with a single abstract method e.g. Comparable, Comparator, Runnable, Callable, ActionListener, etc.  Lambda expression allows passing customized code to the java method that was not possible before java 8. Although before Java 8 the only option was to wrap your code as an object and pass it using anonymous inner class to the method. Lambda expression defines parameters and code that executed using those parameters. Let's take the simplest example of the lambda expression (String input) -> ...

How to install IntelliJ IDEA on Windows?

System Requirements and steps to install IntelliJ IDEA on Windows System Requirements:  Step 1: Download IntelliJ IDEA. Click here. Step 2: Run the downloaded exe file and click on next. Step 3: Check the checkboxes mentioned in the below image and click on next and then hit the install button. Step 4: Final step is to reboot your system before start using IntelliJ IDEA. Thank you, guys. Please do comment below if there is any query or feedback.