×

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in a superclass may only be accessed by subclasses in the other package or by any class inside the package of the protected members' class.

Class and interfaces are exempt from the protected access modifier. Although methods and fields can be declared protected, an interface's methods and fields cannot.

Protected access allows the subclass to access the helper variable or method while prohibiting attempts by unrelated classes to do so.

One of Java's access modifiers is referred to by the protected keyword. The protected methods or data members can be accessed from

  • like the same class.
  • similar packages' subclasses.
  • various classes inside the same packages.
  • various package subclasses.

Conditions for Accessing Protested keyword

  1. Not extending the parent class when calling a protected function reaching a restricted class.
  2. Utilizing a separate but similar package to access the display function.
  3. Utilizing the display feature of a different package.
  4. Using an override to access a protected class inside the same package.

Important Points to remember about protected keyword:

  • Inheritance must be used if one wants to access a protected modifier outside of a package.
  • When a function Object () {[native code]} is protected, users are prevented from creating a class instance outside of the package.
  • If a variable or method is protected during overriding, it can only be overridden by another subclass using the public or protected modifier.
  • Interfaces and outside classes are not protected.
  • The package includes access to the protected access modifier. It is also accessible outside of the package, but only via inheritance.
  • We are unable to give outside classes and interfaces protected status.
  • Creation of an instance of a class from outside the package is not possible if you make any constructors protected.
  • 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 preceding statement, if you give any method or variable the protected access modifier, only the public or protected access modifiers may be used to override it in a subclass.

Examples of Protected Keyword

Example 1:

An illustration of how to check whether a protected variable is usable outside of the package.

//save by All.java  
package com.java.


public class All { 
 protected String msg="class protected is outside the class after accessing";
}  
//save by ProtectedExample1.java  
package com. Java lang;
import com.java. All;


public class ProtectedEx1 {  
public static void main (String [] s) {  
    All am=new All();
   System.out.println(am.msg);
}  
}  

Output:

Error: Unresolved problem

Example 2:

an illustration of how to check whether a protected variable is available outside of the class and within the package.

class All { 
 protected String msg="class protected is outside the class after accessing";
} 
public class ProEx2 {  
public static void main (String [] s) {  
    Allam=new All();
   System.out.println(am.msg);
}  
}  

Output:

class protected is outside the class after accessing

Example 3:

an illustration to implement whether the protected method is available outside of the package or not.

package com.java.
public class All {  
protected void mss ()  
 {  
     System.out.println("method of protected is accessedoutside the package class");
 } 
}  
//save by ProtectedEx3.java  
package com. Javalang ;
import com.java. All;


public class ProtectedEx3 {  
public static void main (String [] s) {  
    All am=new All ();
   am.mss ();


}  
}  

Output:

Error: Unresolved compilation problem

Example 4:

an illustration of how to use inheritance to determine whether a protected method is available outside of the package.

package com.java.
public class All {  
protected void mss ()  
 {  
     System.out.println("method of protected is accessed outside the package classby impilementing inheritance");
 }  
}  
//save by ProtectedEx4.java  
package com. Javalang;
import com.java. All;


public class ProtectedEx4 extends All {  
public static void main (String [] s) {  
    ProtectedEx4 am=new ProtectedEx4();
   am.mss ();
}  
}  

Output:

method of protected is accessed outside the package classby implementing inheritance

Example 5:

protected class ProtectedEx5 {  
void disp ()  
{  
    System.out.println("accessing the outer protected class");
}  
public static void main (String [] s) {  
    ProtectedEx5 paa=new ProtectedEx5();
paa. disp();
}  
}  

Output:

Error: Unresolved problem compilation

Related Topics

Method chaining in Java

Method chaining in Java is the act of calling a series of methods one after the other. The sole distinction between it and function Object () constructor chaining is the difference...

3 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Check whether a Number is a Power of 4 or Not in Java

There are many ways to figure out if an integer is a power of 4. This section will go over a variety of techniques for figuring out whether or not...

11 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.

Java Enumeration

In a computer language, enumerations express a set of named constants. For instance, the four suits in a deck of playing cards could be represented by the enumerators Club, Diamond,...

4 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.