Public Vs Private JavaIn 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:
The following table shows the access level to members permitted by the public and private modifiers. 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). 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. Let's discuss it in detail. Public Access ModifierIt 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 ModifierIt 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.
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: Difference Between Public and Private Access Specifier in JavaThe major difference between public and private modifiers is its visibility. Java categories the visibility for class members as follows:
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. Next TopicWhat is Java Used For |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India