public vs private java
Java has 4 access modifiers:
1. public
2. private
3. protected
4. default.
In this article, we will understand the difference between public
and private
access modifiers in java with example code and explanation.
public access specifier can be applied before a class, method and instance variables with
public
keyword. It is the least restrictive in terms of visibility.
A class, method or variable with public
access is visible everywhere. Everywhere means,
1. Outside the class,
2.Outside the package,
3. Outside the application(to the JVM)
This is the reason why main method is public, so that it can be invoked by the JVM at application startup.
Below is an example of a class with public access, having public
field and public
method.
public class Employee { public String name; public String getName() { return name; } }
Note that class fields or instance variables should not be public
as it violates encapsulation and data hiding.
private
private is the most restrictive access modifier. It may be applied to
1. Inner classes,
2. Fields,
3. Constructor,
4. Methods.
Anything marked private is accessible only within the same class, not even to the child class. Example,
public class Person { private String name; public String getName() { return name; } }
Above class defines a private field. This field is directly accessible within the same class only.
Consider a child class of the above class.
public class EmployeeDetail extends Employee { void printName() { // COMPILER ERROR: name is not visible super.name; } }
When it tries to access parent class field directly, a compiler error is raised. Same applies to private methods.
A top level class can not be marked private. Top level class means a class which is defined in its own file.
So, below class definitions are valid.
// Top Level Class public class Employee { // Inner Class private class EmployeeDetail { } } class Shape { }
private is valid for inner classes.
Private constructor
A constructor in java is used when creating object of a class. Constructor is implicitly invoked when an object of a class is created.
So, if a constructor of a class is marked private
, you can not create its object outside the class. Example,
public class PrivateConstructorDemo { // private constructor private PrivateConstructorDemo() { } public PrivateConstructorDemo getObject() { // allowed return new PrivateConstructorDemo(); } } // other class class Driver { void testMethod() { // error new PrivateConstructorDemo(); } }
When trying to create an object of a class with private constructor outside the class, the compiler will complain.
public vs private
From the discussion till now, it is clear that the difference between public and private access modifiers lies in the visibility.
Below is a table which summarizes the difference between the visibility of these two.
Comparison Basis | Public | Private |
---|---|---|
Applicable To | Classes, Interfaces, Fields, Methods, Constructor | Can not be applied to top level Classes and Interfaces. Can be applied to Fields, Methods, Constructor |
Visible in same package | Yes | No |
Visible to other package | Yes | No |
Visible to child class in same package | Yes | No |
Visible to child class in different package | Yes | No |
Recommended for class fields | No Violates encapsulation and data hiding. |
Yes |