1/* similar to private keyword, but also lets both:
2 - subclasses
3 - classes in same package
4 access the variable, method or constructor */
5
6class Superclass {
7 protected int myNumber = 5;
8}
9
10class Subclass extends SuperClass {
11 // has access to myNumber
12}
13
14class InAnotherPackage {
15 // doesn't have access to myNumber
16}
1//The protected keyword is an access modifier used for attributes,
2//methods and constructors,
3//making them accessible in the same package and subclasses.
4
5class Person {
6 protected String fname = "John";
7 protected String lname = "Doe";
8 protected String email = "john@doe.com";
9 protected int age = 24;
10}
11
12class Student extends Person {
13 private int graduationYear = 2018;
14 public static void main(String[] args) {
15 Student myObj = new Student();
16 System.out.println("Name: " + myObj.fname + " " + myObj.lname);
17 System.out.println("Email: " + myObj.email);
18 System.out.println("Age: " + myObj.age);
19 System.out.println("Graduation Year: " + myObj.graduationYear);
20 }
21}