Protected keywords are keywords that are used to restrict the scope within which the variable, method, and constructors can be accessed. It is one of the types of access modifiers in Java. They are used to differentiate between the scope of methods, variables, constructors, and classes. There are 4 types of access modifiers in Java, and they are:
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsOnce a variable or a method is marked as protected, it can only be accessed by the below methods:
Protected keywords are like a combination of both public and private keywords since they were introduced to access the variables outside the class (which is not possible in the case of private keywords) and maintain that only certain methods can inherit the same.
Protected keywords are declared with the keyword prefixed to them as “protected”. We first declare the protected keyword in one of the class called “MyClass” as below:
class MyClass { protected String name = "Katy"; protected int token= 55; } public class SubClass extends MyClass { public static void main(String[] args) { SubClass obj = new SubClass(); System.out.println(obj.name + "'s token number is: " + obj.token); } }
Here the class “SubClass” extends “MyClass”, and hence the protected keyword can be used here by creating an object of SubClass and by calling the variables.
Output:
Protected keywords can only be used at the member level, i.e. inner classes declared outside of a function and non-static. Protected keyword is different from that of private as they can be accessed outside of a class and in the subclass of another package.
Some of the restrictions on using protected keywords are:
Let us go through some examples where we can understand the concept of protected keywords better.
Here we try to call the keyword from the parent class of “package1”. “ProtectedExample2” is created in “package2”, and the keyword “disp” is called here. But the code will not be able to access the keyword since the child class has not inherited its value from the main class and will throw an exception as shown.
Code:
package com.package1; public class Example { protected String disp="Printing message from protected variable from package1"; } //Create new package as com.package2 //Create new class as ProtectedExample2 package com.package2; import com.package1.Example; public class ProtectedExample2 { public static void main(String[] args) { ProtectedExample2 a=new ProtectedExample2(); System.out.println(a.disp); } }
Output:
In this example, we try to access the class “ProtectedExample5”, which is protected. This causes a compilation error.
Code:
protected class ProtectedExample5 { void display() { System.out.println("Try to access outer protected class"); } public static void main(String[] args) { ProtectedExample5 p=new ProtectedExample5(); p.display(); } }
Output:
In the below example, we first create a package called “com.package1” and create a new class by the name “Example”. Here we declare our keyword “disp” is protected. We shall try to display this protected keyword using the class “Example1”. For this, first, an object of parent class “Example1” needs to be created and then print the value assigned to the keyword “disp”.
Code:
package com.package1; public class Example { protected String disp="Printing message from protected variable from package1"; } class Example1 { public static void main(String[] args) { Example obj=new Example(); System.out.println(obj.disp); } }
Output:
Using the same code as above, we shall see how to call the protected keyword by creating a different package, “package2”. A protected keyword can be accessed only through inheritance from package1; hence “ProtectedExample2” is extended from “Example”. In a similar way as the first example, we have to create an object of the class “ProtectedExample2” in order to access the protected keyword from package “com.package1”.
Code:
package com.package2; import com.package1.Example; public class ProtectedExample2 extends Example{ public static void main(String[] args) { ProtectedExample2 a=new ProtectedExample2(); System.out.println(a.disp); } }
Output:
Here the class is declared as protected inside the inherited class “Example5”. Also, a protected class called “Example” is declared outside the function but in the same package. When an object of “Example5” is created and the protected class “disp()” is called, we can observe that the overridden method is called instead of the outside class. This is because we shall not be able to import “com.package1” and its class “Example” since it is not visible and causes a compilation error.
Code:
//Create a file by Example.java package com.package1; class Example { protected void disp() { System.out.println("Printing from protected class in the outside function"); } } //Create a class by the name Example5.java public class Example5 extends Example { protected void disp() { System.out.println("Accessing the overriden function"); } public static void main(String[] args) { Example5 exp=new Example5(); exp.disp(); } }
Output:
The importance of protected keyword in java is:
As shown in the above examples, we choose protected keywords depending on the access level we require at the code level. They help greatly in cases where the same variable or class needs to be accessed from other inherited methods in the program. A parent-child relationship is always present between the parent class and its sub-classes which are using the protected keyword.
The above is the detailed content of Protected Keyword in Java. For more information, please follow other related articles on the PHP Chinese website!