×

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

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

Sum of digits in string in java

To find the sum of all digits in a string, you need to traverse through the string one by one character; if the character is an integer, you need to...

2 minutes read.

Java Math asin() Method

The asin() method of Math class computes the trigonometric Arc Sine (inverse of sine ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double asin(double a) Parameters: The...

1 minute read.

Constructor in Java with Example

Java Constructor  The constructor is used for object initialization. It's a block of code that initializes a newly created object. It contains a collection of statements that are executed at the...

5 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Can we override Private Method in Java

In this article we will learn the concept of override, private method in java and we will now whether we can override private method in java or not. Override in Java Any...

3 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 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.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

How to resolve Illegal state exceptions in Java

What is IllegalStateException? When a method is called at the incorrect time, a runtime exception called an IllegalStateException is raised in Java. This exception is utilized to show that a method...

3 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

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.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes 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.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.