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
- Not extending the parent class when calling a protected function reaching a restricted class.
- Utilizing a separate but similar package to access the display function.
- Utilizing the display feature of a different package.
- 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