Javatpoint Logo
Javatpoint Logo

Public Vs Private Java

In Java, public and private are keywords that are known as an access modifier or specifier. It restricts the scope or accessibility of a class, constructor, variables, methods, and data members. It depends on which it is applied. Java provides the four types of access modifiers: public, private, protected, and default. But in this section, we will discuss only two public and private, and also discuss the difference between public and private access specifier with example.

Access modifiers control whether other classes can use a particular field or invoke a particular method. Java provides two levels of access control:

  • Top-Level: At this level, we can use only a public
  • Member Level: At this level, we can use public, private, protected and package-private (if we use no explicit modifier is known as package-private).

The following table shows the access level to members permitted by the public and private modifiers.

Public Vs Private Java

Still not clear the differences between the two? Let me show you a figure that demonstrates how access levels affect visibility. The following figure shows the two packages p1 and p2. Each package contains two classes Demo1 and Demo2 (in package p1), and Demo3 and Demo4 (in package p2).

Public Vs Private Java

The following table describes the visibility of the classes if we make the classes public and private one by one. In the above figure the Demo1 is the only class that is visible for each access modifiers.

Public Vs Private Java

Let's discuss it in detail.

Public Access Modifier

It can be specified by using the public keyword. Its scope or accessibility is the widest among other access specifiers. The variables, classes, and methods declared as public can be accessed from everywhere in the program. It does not impose restrictions on the scope of public data members. If we declare methods and classes as public, they also violate the principle of encapsulation. We can also use it with the top-level classes.

Let's use the private access specifier in a Java program for better understanding.

Demo1.java

Demo2.java

Output

Javatpoint

Private Access Modifier

It is the opposite of the public modifier. It can be specified by using the private keyword followed by class name (applied only on nested classes) or method name or data member. We cannot use the private access specifier with the top-level classes or interfaces. The variables, methods, and classes declared as private can be accessed only in the class in which they are declared or by inheriting the parent classes. It is the most restricted access specifier in Java. It is the heights form of encapsulation.

  • We cannot override the methods that are declared as private.
  • If we use the private modifier with the constructor, we avoid it from being sub-classed.
  • It hides the classes from the other classes within the same package.

Let's use the private access specifier in a Java program for better understanding.

In the following example, we have declared two classes: Demo1 and Demo2. In the class Demo1, we have defined a method show() as private. The class Demo2 contains the main() method in which we have created an object of the class Demo1. After that, we are trying to access the private method of the class Demo1 from the class Demo2, that is not possible. But still, we will execute the program to see which error it shows.

When we execute the above program, it shows the following error:

Public Vs Private Java

Difference Between Public and Private Access Specifier in Java

The major difference between public and private modifiers is its visibility. Java categories the visibility for class members as follows:

  • Subclasses in the same package
  • Non-subclasses in the same package
  • Subclasses in different packages
  • Classes neither in the same package nor subclasses
Public Vs Private Java

Which one we should use?

We should use public access modifier if we want to make the method or property visible from anywhere, other classes, and instances of the object. Use the private access modifier if you want to make the method or property visible in its own class only. Avoid public fields except for constants.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA