Now that we have understood the concept of inheritance, protected this keyword finally has meaning. In an ideal situation, private members are "private" at any time, and no one is allowed to visit them. In practice, however, it is often possible to hide something deeply, but at the same time allow access to the members of the derived class. The protected keyword can help us do this. It means "it is private, but it can be accessed by anything inherited from this class or anything else in the same package." In other words, protected in Java can become "friendly".
The best we can do is to maintain the private state of the members-and, in any case, retain the right to modify the implementation details of the foundation. In this context, the protected method allows the inheritors of a class to have controlled access:
: Orc.java
//The Protected keyword
import java.util.*;
Class Villain {
private int i;
protected int Read () {return i;}
protected void Set (int ii) {i = II}
Public villain (int ii) {i = II}
public int value (int m) {return m*i}
}
public class Orc extends villain {
private int J;
Public ORC (int JJ) {super (JJ); j = JJ;}
public void change (int x) {set (x);}
} ///:~
As you can see, the change () has access to set () because its properties are protected (protected).