The public and protected access modifiers determine how members of a class or method are accessed. Modifiers are attached to members when declared. We know that these access modifiers play an important role in Java oops concepts like encapsulation, polymorphism and inheritance. It helps prevent abuse of membership-provided features. We will try to understand public and protected access modifiers in Java with a sample program.
Java does not restrict the accessibility of public members. Anything declared as public can be accessed anywhere, which means we can access them inside the class, outside the class, inside the package and outside the package. You may have noticed that the main() method in Java is always defined as a public method so that it can be called by any JVM outside the scope of the current program.
Some examples of public access modifiers -
public int i1 = 108; public double d2 = 6.55;
Here, the variable is declared as public.
The following example illustrates how package members work in Java.
class Pack { public void prnt() { // method declared as public String msg = "I am inside a public method"; System.out.print(msg); } } public class ClassShow extends Pack { // public child class public static void main(String args[]) { // creating object of child class ClassShow obj = new ClassShow(); // method calling through object of child class obj.prnt(); } }
I am inside a public method
In the above code, the "Pack" class is the parent class of "ClassShow". In the parent class, we declared a public method called "prnt()" to print a simple message. In the main() method of the subclass, we define an object of the subclass "ClassShow" to call the public method "prnt()". Here, the subclasses are also public.
Mostly used in the case of inheritance to control access to parent class members and corresponding subclass members. It allows access to elements outside the current package, but only direct subclasses of the class. Here, a package is a container that holds a set of classes.
Some examples of protected access modifiers -
protected int data1 = 5; protected double data2 = 5.55;
Here, the variable is declared protected.
The following example illustrates the use of protected methods in Java.
class Pack { protected void prnt() { String msg = "Tutorials Point!!"; System.out.print("Welcome to " + msg); } } public class ClassShow extends Pack { public static void main(String args[]) { // creating object of child class ClassShow obj = new ClassShow(); // method calling through object of child class obj.prnt(); } }
Welcome to Tutorials Point!!
In the above code, the "Pack" class is the parent class of "ClassShow". In the parent class, we declare a protected method called "prnt()" to print a simple message. In the main() method of the subclass, we define an object of the subclass ‘ClassShow’ to call the protected method ‘prnt()’.
From the above discussion, we can derive the following differences between public and private access modifiers -
public |
protected |
---|---|
We need to use the keyword "public" to specify that the member is public. |
We use the "protected" keyword to specify that members are protected. |
We can define any class as a public class. |
Class cannot be defined as protected. |
Public members can be accessed by any class inside and outside the package. |
Protected members can be accessed within the package as well as from other packages. But for other packages, it can only be accessed by inherited classes. |
Applicable to both top-level and members. |
Available only at membership level. |
We first defined the public and protected access modifiers, and in the following sections, we explained them in detail with respective examples. Finally, we discuss some of the differences between them.
The above is the detailed content of The difference between public and protected access modifiers in Java. For more information, please follow other related articles on the PHP Chinese website!