Protected Keyword In Java
Protected is an access modifier hence we can use it to make a class, attribute, method, etc protected.
A protected keyword is used when we want to make something only accessible within the boundary of the same package, and same subclass.
Protected is a reserved word hence it will always be written in lower case.
Now we will see an example -
Example 1 -
package javaLearnings;
class T{
protected int a = 10;
protected int b = 20;
protected int c = a+b;
}
public class Main {
public static void main(String[] args) {
T on = new T();
System.out.println("the sum of "+on.a+", and "+on.b+" is = "+on.c);
}
}
|
Output -