Henry M
0
Q:

java protected

/* similar to private keyword, but also lets both:
  - subclasses
  - classes in same package
  access the variable, method or constructor */

class Superclass {
  protected int myNumber = 5;
}

class Subclass extends SuperClass {
  // has access to myNumber
}

class InAnotherPackage {
  // doesn't have access to myNumber
}
3
class Person {
  protected String fname = "John";
  protected String lname = "Doe";
  protected String email = "[email protected]";
  protected int age = 24;
}

class Student extends Person {
  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);
  }
}
1

New to Communities?

Join the community