Java Private keyword
A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained.
Privately declared constructors, variables, and methods are only accessible within the declared class. The most limited access level is private access modifier. Interfaces and classes cannot be private. If the class contains public getter methods, private variables can be accessed from outside the class. The primary means by which an object isolates itself and conceals data from the outside world is using the private modifier.
In a Java code block, the word "private" is used to indicate that a class, function, or variable has limited accessibility. A class, method, or variable in a program that is designated as private signifies that, unlike a public method, it cannot be accessed by anybody outside of the class or method. Java normally uses the Private keyword in a fully contained class.
Working of Private Keyword
Java's "Private Keyword" function only applies to that class. Outside of the class(es) and interface(s), it is inoperative. If the class members are PRIVATE and the class is entirely enclosed, the Private Keyword works nicely.
Using some access modifiers, a private keyword, variable, or method can also be overridden by a subclass or subclasses to call a PRIVATE METHOD outside of the class. By utilising simply, the Private Access Modifiers, Private Keyword can now also be used outside of the class.
Important Points about Private Keyword:
- Only members of the same class have access to the private access modifier.
- We are unable to make an interface and outer class private.
- The best way to use the private keyword in Java is to entirely encapsulate a class by making all its data members private.
- Making a class' function Object () {[native code]} private prevents others from making instances of that class.
- Any method that is being overridden must not be more restrictive than the original method, which must be stated in the subclass.
- In accordance with the previous point, any method or variable that has a private modifier applied to it can be overridden by a subclass using any form of access modifier. We are still unable to call private methods outside of the class, though.
Examples of Java Private Keyword
Example 1:
Using an example, you can tell whether a private variable is available outside of the class or not.
class AP
{
private String mss="accessing the private variable outside the class";
}
public class PrivateEx1 {
public static void main (String [] s) {
AP a=new AP ();
System.out.println(a.mss);
}
}
Output:
Error: Unresolved compilation problem:
The field A.msg is not visible
Example 2:
Using an example, you can determine whether a private method is available outside of the class.
class AP
{
private void disp ()
{
System.out.println("accessing private keyword ouside of the class ");
}
}
public class PrivateEx2 {
public static void main (String [] s) {
Ap a=new AP ();
a. disp ();
}
}
Output:
Error: Unresolved problem compilation
Example 3:
Take the following example into consideration to determine if we can grant the outer class the private modifier.
private class PrivateEx3 {
void disp ()
{
System.out.println("accessing outer private class");
}
public static void main (String [] s) {
PrivateEx3 p=new PrivateEx3();
p. disp();
}
}
Output:
Error: Unresolved problem of compilation
Example 4:
The private method cannot be called from outside the class, as demonstrated in the example above. By altering the runtime behavior of the class in this case, we can invoke the private function from outside the class.
import java. langs. reflect. Method;
class AA {
private void disp()
{
System.out.println("implementation of private method is done");
}
}
public class PrivateEx4{
public static void main (String [] s) throwsException {
Class c = Class.forName("AA");
Object o= c. newInstance ();
Method mm =c. getMethod ("dispplaying", null);
mm.setAccessible(true);
m.invoke (o, null);
}
}
Output of the program is given by:
Implementation of private method is done
Example 5:
To determine whether we use the private function Object () { [native code] } instance outside of the class, consider an example.
class AA
{
String mss;
private AA(String mss)
{
this.mss=mss;
}
void disp()
{
System.out.println(mss);
}
}
public class PrivateEx5 {
public static void main (String [] s) {
AAhh=new AA ("implementing the constructors outside the class");
hh. disp ();
}
}
Example 6:
In this illustration, the default access modifier is used to subclass the private method. But even so, calling parent class methods from subclasses is not permitted.
class AA {
private void mss ()
{
System.out.println("method of parent class");
}
}
public class PrivateEx6 extends AA {
void mss ()
{
System.out.println("method of child class");
}
public static void main (String s []) {
PrivateExample6 obj=new PrivateEx6();
obj.mss ();
}
}
Output:
Method of child class