Core Java

Protected Java Keyword Example

In this article, we talk about protected Java keyword and Java access Modifiers.

1. Introduction

A Java protected keyword is a Java access modifier. It can be assigned to variables, methods, constructors, and inner classes.

  • The protected access modifier is accessible within the package. However, it can also accessible outside the package but through inheritance only.
  • We can’t assign protected to outer class and interface.
  • If you make any constructor protected, you cannot create the instance of that class from outside the package.
  • If you are overriding any method, the overridden method (i.e., declared in the subclass) must not be more restrictive.
  • According to the previous point, if you assign protected to any method or variable, that method or variable can be overridden to sub-class using a public or a protected access modifier only.

2. Java Access Modifiers table

The following table clearly explains about four access modifiers namely public, private , protected and default and their availability for an access across same package or outside package or in subclass or non-subclass.

Protected Java - Access Modifiers table
Fig. 1. Access Modifiers table

Public: This modifier can be accessed from anywhere within the same package or outside the package, within the same class or outside the class.

Private: This modifier can be accessed only within the same class of the same package. It is not accessible anywhere else.

Protected: This modifier can be accessed anywhere within the same package, but in the different packages, it is accessible only by its child classes.

Default: This modifier can be accessed anywhere within the same package, but in the different packages, it is not accessible anywhere else.

3. Protected Java Keyword Example

Let’s understand protected keyword with the help of an example:

In this example, we will create two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method display in class A is protected and class B is inherited from class A and this protected method is then accessed by creating an object of class B.

A.java

// Java program to illustrate 
// protected modifier 
  
package p1; 
  
// Class A 
public class A { 
    protected void display() 
    { 
        System.out.println("JavaCodeGeeks"); 
    } 
} 

B.java

// Java program to illustrate 
// protected modifier 
  
package p2; 
  
// import all classes in package p1 
import p1.*; 
  
// Class B is a subclass of A 
class B extends A { 
    public static void main(String args[]) 
    { 
        B obj = new B(); 
        obj.display(); 
    } 
} 

When we compile and run B.java then

Output

JavaCodeGeeks

Need of Protected Keyword: Protected keyword can be used for a member in java, if we want that member accessible throughout the same package where it is declared and also it should be available to its child classes in other packages.

We have clearly used the protected method which was declared in class A of package p1 and was available to its child class B in another package p2.

4. Summary

In this post, we have started with the protected keyword in java, its usage, then we have seen a table that gives information about various java access modifiers like public, protected, default, and private and their accessibility. Then we finally understood protected keyword with an example.

5. Download the Source Code

This is an example of protected keyword in java.

Download
You can download the full source code of this example here: Protected Java Keyword Example

Shaik Ashish

He completed his Bachelors Degree in Computer Applications. He is a freelancer, writer, Microsoft Certified in Python and Java. He enjoys Writing, Teaching, Coding in Java and Python, Learning New Technologies and Music.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button