Protected keyword in java

Protected keyword in java is an access modifiers in java .Protected keyword acts like private, with the exception that an inheriting class has access to protected members, but not private members.

Example of Protected keyword


class Protectedkeyword{
  protected String fname = "John";
  protected String lname = "Doe";
  protected String email = "[email protected]";
  protected int age = 24;
}

class Student extends Protectedkeyword{
  private int graduationYear = 2018;
  public static void main(String[] args) {
    Student myObj = new Student();
    System.out.println("Name: " + myObj.fname + " " + myObj.lname);
    System.out.println("Email: " + myObj.email);
    System.out.println("Age: " + myObj.age);
    System.out.println("Graduation Year: " + myObj.graduationYear);
  }
}

Leave a Comment