Java Protected Attributes

Java Protected Attributes


Protected Attribute 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 - 
 
 
Example2  -  Now we will see What will happen when we try to access
 
 
And second class is here - 
 
 
Here we can see that the class is public but the data inside in protected 
Output -