What is the Difference between Java Public vs Private?
Java has four different access modifiers: default, public, private, and protected. In this article, we are mainly going to focus on public and private access modifiers. We will look at how Java public and Java private are different from each other with examples. We will also look at which one you should use according to your needs.
What are Access Modifiers?
We can understand access modifiers with real-life examples. For example, you might tell the name of the company you work for to all your relatives and friends, but information regarding your bank account details is only known to you or your very close ones. Some information is known to everyone, and on the other hand, some information is restricted to only certain people. So, in very simple terms, access modifiers in Java are simple tools to control the accessibility and visibility of classes, methods, constructors, etc. We can change how much data and which data is visible inside the package containing the class or method or constructor to which we are adding our modifier as well as outside the package, like classes belonging to a different package.
Java has 4 types of access modifiers:
- Default: As the name suggests, default is the default modifier if no other modifier is provided. Any class, method, or constructor with a default modifier is only accessible within the class and is not visible or accessible from outside the class.
- Public: Public is the most accessible modifier. Any class, method, or constructor with the public access modifier is visible from anywhere, even from classes that belong to a different package.
- Private: The private access modifier provides the most restrictive accessibility; it can only be accessed within the class.
- Protected: The protected access modifier also provides restrictive accessibility but is more accessible than the private modifier. It is visible and accessible within the class and outside the package, but only to the protected class's subclasses.
You can reference the following table for a better understanding of the scope of each access modifier.
Default | Private | Protected | Public | |
---|---|---|---|---|
Same class | Yes | Yes | Yes | Yes |
Same package subclass | Yes | No | Yes | Yes |
Same package non-subclass | Yes | No | Yes | Yes |
Different package subclass | No | No | Yes | Yes |
Different package non-subclass | No | No | No | Yes |
Access modifiers are a very important part of Java programming because they give you the power to decide which data should be available to everyone and which data, classes, or methods should be hidden or restricted. They make your code much more organized, safer, and more protected.
What are Public Access Modifiers in Java?
Java public access modifier is a modifier that does not restrict the class, method, or constructor to which it is applied. It's like information that you share with everyone, so it is used with that part of the code that you want to be visible and accessible to everyone. Any class/method/constructor with a public access modifier is accessible from anywhere in the code, even from outside the package and from any class. Thus, it has the most flexible scope of all the access modifiers.
Let's try to understand the public access modifier with the help of a java program.
The output of this code looks something like this:
In this example, we are creating a public class called Car with a public variable tyreCount and a public method display. As you can see in the above example since the class car is marked public, it can be accessed from a different class called Main, and we can only access its public variables and methods.
In the above example, the public class Car was accessed from the main class. public variable tyreCount was accessed from the main class public method display was accessed from the main class.
In Java, the public access modifier is used whenever we want a particular class, method, constructor, or variable to be accessible from all the other classes and methods.
What are Private Access Modifiers in Java?
If you don't want something visible outside the current class, use a private access modifier. As the name suggests, the private access modifier provides the most restriction on accessibility compared to other access modifiers. Any data member, such as a class, a method, or a constructor, that is prefixed with the private access modifier won't be visible or accessible outside of the current class.
Let us understand the private access modifier with the help of a java program.
Let's see the output of the above code:
As you can see, we get this error: word has private access in dummyClass. Let's try tweaking our code a little bit and see if it works.
Let's see what the output of this code looks like.
Now let's see what happens in both the java programs and why the second Program works perfectly while the first program throws an error.
In the first code, we declare a private variable by the name of a word that is part of a class named dummyClass. By nature, any data member that is marked as private is only accessible inside the class to which it belongs. So that is why when we create an object of the dummyclass inside of another class named Main, and try to access its private variable, our program threw an error saying Main.java:23: error: word has private access in dummyClass.
But in the second code, the private variable word is declared inside of the Main class itself, so when we try to access it from the main method, it does not throw an error, and our program works perfectly.
Difference between Java Public and Private Access Modifiers
Public Access Modifier | Private Access Modifier |
---|---|
Both top-level class and Member-level classes can be declared as public | top-level classes and member-level classes cannot be declared private; only member-level data members can be declared private. |
Any child class within the same package can access the public members of the class | Private members of a class are not allowed to be accessed from the child class that belongs to the same package. |
The non-child class that belongs to the same package can access the public members of the class | Private members of a class are not allowed to be accessed from the non-child class that belongs to the same package. |
Any child class from outside the current package can access the public members of the class | Private members of a class cannot be accessed from the child class of any outside package. |
A non-child class belonging to a different package outside of the current package can access the public members of the class. | Private members of a class are not allowed to be accessed from a non-child class belonging to a different package outside of the current package. |
Out of all the access modifiers, the public access modifier is the most accessible one. | the private access modifier is the most restrictive of all the access modifiers. |
A public access modifier is recommended for methods. | Private access modifier is recommended for data members of a class or a method. |
Java Public vs Private: Which One We Should Use?
After looking at what private and public access modifiers are, their scope, and differences, the Answer to this question comes down to how you wanna structure your program, what details you want to be accessible from anywhere, and which details you want to be restricted.
It is recommended that you use public access modifiers while working with methods that you want to be visible to all the other classes, even from outside the current package. But at the same time, declaring the data members within the method or the class as private so that they are only visible in the class in which they are declared. This makes your program clean and organized, as all the unnecessary information is hidden away, and it makes your program much more secure.
If you want to learn more about classes, packages, access modifiers, their scopes, their differences, and how to use them, you can check out these articles -
Explore Scaler Topics Java Tutorial and enhance your Java skills with Reading Tracks and Challenges.
Conclusion
- JVM needs information regarding the accessibility of different classes, methods, or data members in the program.
- In java, access modifiers are the tool to define the scope of accessibility for different classes, methods, and data members.
- There are four types of access modifiers in Java: default, private, protected, and public.
- public access modifier is the most accessible access modifier. a class, method, or data member marked as the public is visible everywhere, even from a class outside the current package.
- The private access modifier is the most restrictive. Any class, method, or data member marked as private is only visible in the class to which it belongs.
- It is recommended that you use the public access modifier with classes and methods that you want to be visible from anywhere and make their data members private to make your code organized and secure.