Home> Java> javaTutorial> body text

The difference between public and protected access modifiers in Java

WBOY
Release: 2023-09-17 20:49:02
forward
735 people have browsed it

The difference between public and protected access modifiers in Java

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.

Access modifiers in Java

Public access modifier

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;
Copy after login

Here, the variable is declared as public.

Example

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(); } }
Copy after login

Output

I am inside a public method
Copy after login

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.

Protected access modifier

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;
Copy after login

Here, the variable is declared protected.

Example

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(); } }
Copy after login

Output

Welcome to Tutorials Point!!
Copy after login

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()’.

Public and protected access modifiers

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.

in conclusion

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!