Open-Source Internship opportunity by OpenGenus for programmers. Apply now.
In the article, we have covered the differences between public, private, protected and no modifier in Java in detail.
In short, following image summarizes the differences:
Differences
-
First and important difference is the accessibility i.e. anything public is accessible to anywhere , anything private is only accessible in the class they are declared , anything protected is accessible outside the package but only to child classes and default is accessible only inside the package.
-
Another difference between public and private modifier is that of Encapculation. Public modifier provides lowest level of Encapculation and Private modifier provides higher level of Encapsulation in Java.
-
Another difference is that you can use public modifier with top level class but you cannot make a top level class private in java.You can make inner class private.
-
Another difference is that default is package level accessibility i.e. if you don't provide any access modifier to a class, method or variable then Java by default make them accessible inside the package.
-
Another difference between protected and default modifier is that protected modifier provides more accessibility than default modifier.You can access a protected member outside the package, but only inside sub classes.
That's all about difference between public,private,protected and no modifier in Java.
To understand this deeply, we will go through important terms like class, subclass, package and others and then, understand each access specifier with code examples.
Important terms
A class defines the characteristics and behaviour of an object.For example,if you create a gaming application,each game that a user can play from the aplication can be considered as an object of the Games class.Each game has common characteristics,such as the number of players,game category and score.These characteristics are known as member variables
and behavior is specified by methods
. A class defines the member variables and methods of objects that share common characteristics. Further,all the games have common methods, such as calculating score,starting the game and displaying game instructions.
Syntax:
class <ClassName>
{
//Declaration of member varaibles
//Declaration of methods
}
Subclass:-
A Java subclass is a class which inherits a method from a superclass(the class from which subclass is derived).To create a subclass of another class, use the extends clause in your class declaration.As a subclass, your class inherits member variables and methods from its superclass.
Object:-
An object is an instance of a class and has a unique identity.The identity of an object distinguishes it from other objects.Classes and objects are closely linked to each other. Declaring an object creates a variable that will hold the reference to the object.Īn this, new operator is used, which allocates memory to an object.
Syntax:
object_name= new class_name();
Package:-
A package is a collection of classes. A package provides the space essentially used to organize classes that are related to each other.You can create multiple packages to organize multiple categories of classes as per the requirement.A class can belong only to one package.For example, a package named Game can be used to create and store a class named Game1.This will ensure that the Game1 class does not conflict with any that has the same name and is stored somewhere else.
Syntax:
package <package_name>;
Access Modifier
In Java, there are number of Access Modifiers to control the access of class members. The various types of access modifiers in Java are:
- Public
- Private
- Protected
- Default or No modifier
Public Modifier
The members of a class that are preceded with the public
modifier are accessible by the classes present within the package
or outside the package
.
You can access a public class,data member or method within the class in which they are defined, from the classes defined in the same package
or outside the package
.The following code snippet shows how to declare a data member of a class as public:
public <data type> <variable name>;
The following code shows how the public
modifier can be implementerd in the ClassicGame
class for the start
variable:
class ClassicGame{
public int start;
void show()
{
System.out.println(start);
}
}
In the preceeding code, the start
variable has been specified with the public
modifier. The start
variable can be accessed anywhere in the class ClassicGame
class. It is also accessible in the package
in which the ClassicGame
class is created, or in a package
that is different from the one where the ClassicGame
class exists.
Example:-
package p1;
public class C{
public void show()
{
System.out.println("OpenGenus");
}
}
package p2;
import p1.*;
class D{
public static void main(String[] args)
{
C c1=new C;
c1.show();
}
}
Output:-
OpenGenus
Private Modifier
The private
modifier allows a class to hide its member variables and member methods from other classes.Therefore, the private members of a class are not visible outside a class.They are visible only to the methods of the same class.Therefore, the data remains hidden and cannot be altered by any method other than the member methods of the class.If a variable or methods is declared private
, it can be accessed only within its enclosing class.A top class cannot be declared private
in Java.However, an inner class can be declared private
.The following code snippet shows how to declare a private
data member of a class:
private <data type> <variable name>;
The following code shows how the private
modifier can be implemented in the ClassicGame
class for the score
variable:
class ClassicGame{
private int score;
void show()
{
}
}
In the preceeding code, the score
variable has been specified with the private
modifier.The score
variable can be accessed anywhere in the ClassicGame
class, but it is not accessible to other classes.
Example:-
class Car{
private int type=100;
private void msg(){
System.out.println("Car type");
}
}
public class Hlo{
public static void main(String[] args){
Car c=new Car();
System.out.println(c.type);
c.msg()
}
}
Output:-
Error because msg() has private access in Car
Protected Modifier
The members of a class that are preceded with the protected
modifier are accessible to all the classes within the package
and by the subclasses
outside the package
.The protected
modifier becomes important while implementing inheritance.The following statement shows how to declare a protected
data member of a class:
protected <data type> <name of the variable>;
Consider the following code:
class ClassicGame
{
protected int score;
void show()
{
}
}
In the preceeding code,the score
variable is declared as protected
.It can therefore be accessed within the ClassicGame
class from the classes that will inherit the from ClassicGame
class.It can also be accessed within the classes of the package
that contains the ClassicGame
class.
Example:-
public class ProtectedTest{
protected String name="Ankit";
protected int age=20;
protected String info(){
return name +" is "+ age +" years old";
}
public static void main(String[] args){
System.out.println(new ProtectedTest().info());
}
}
Output:-
Ankit is 20 years old
No Modifier
This is also known as default
modifier.If you do not specify any of the preceeding modifiers, the scope of data members and methods is default
or friendly
.A class, variable or a method with a friendly access can be accessed only by the classes that belong to the package
in which they are present.
Example:-
public class DefaultTest{
int age=12;
String name="happy";
String info(){
return name + "is "+ age+" years old";
}
public static void main(String[] args){
System.out.println(new DefaultTest().info());
}
}
Output:-
happy is 12 years old
With this, revisit the differences we presented at the beginning. It will be much more clear. Enjoy.