[Java] Modifier
Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessible from anywhere or not.
whether cild class creation is possible or not, whether object creation is possible or not etc. we can specify this information by using an appropriate keyword in java called access modifiers.
So access modifiers are used to set the accessibility of classes, methods, and other members.
Public Access Modifiers
If a class is declared as public then we can access that class from anywhere.
package modifier;
public class A {
public void m1() {System.out.println("GFG");}
}
package modifier;
//importing package modifier
import modifier.A;
//driver class
class B {
//main method
public static void main(String[] args)
{
//creating an object of type class A
A a = new A();
//accessing the method m1()
a.m1();
}
}
If class A is not public while compiling B class we will get a compile-time error saying modifier.
A is not public in modifier and can’t be accessed from the outside package.
Similarly, a member or method, or interface is declared as public as we can access that member from anywhere.
Protected Access Modifier
This modifier can be applied to the data member, method, and constructor, but this modifier can’t be applied to the top-level classes and interface.
A member is declared as protected as we can access that member within the current package and only in the child class of the outside package.
public class A {
public void m1() {System.out.println("GFG");}
}
//creating a child 'C' class by extending the class A
class C extends A {
public static void main (String[] args) {
//creating an object of parent class using parent reference
A a = new A();
//calling method m1
a.m1();
//creating an object of child class using child reference
C b = new C();
//calling method m1
b.m1();
//creating an object of child class using parent reference
A a1 = new C();
//calling m1 method
a1.m1();
}
}
✔︎ In the above example, we create three objects using parent reference and child reference and call m1() method on it, and it successfully executed so from the above example we can say that we can access the protected method within the current package anywhere either by using parent reference or by child reference.
Private Access Modifiers
‘Private’ is not applicable for top-level classes or interfaces. It is only applicable to constructors, methods, and fields inside the classes.
If a variable or methods or constructor is declared as private then we can access them only from within the class i.e from outside the class we can’t access them.
Finally, after getting it done with all four access modifiers let us conclude the evident different between them.
Public Access Modifier | Private Access Modifier |
This is applicable for both top-level classes and interfaces. | This is not capable fro both top-level classes and interfaces. |
Public members can be accessed from the child class of the same package. | Private members cannot be accessed from the child class of the same package. |
Public member can be accessed from non-child classes of the same package. | Private members cannot be accessed from non-child classes of the same package. |
Public members can be accessed from the child class of outside package. | Private members cannot be accessed from the child class of outside package. |
Public members can be accessed from non-child class of outside package. | Private members cannot be accessed from non-child class of outside package. |
Public modifier is the most accessible modifier among all modifiers. | Private modifier is the most restricted modifier among all modifiers. |