The Student subclass accesses a Person class containing protected attributes.
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); } } |
The “protected” keyword is an access modifier utilized for attributes, methods, and constructors, permitting access within the same package and by subclasses.