oop

12. Encapsulation: Access Modifiers in Action

Analyze the code, write your predicted output, and then reveal the solution.

Scenario

How do access modifiers (private, protected, public) affect what is inherited and accessible in a subclass?

Code Block
package hospital; class Employee { public String name = "Global"; protected double salary = 50000; private String healthID = "HID-123"; int vacationDays = 20; // Default (Package-private) } class Doctor extends Employee { void printDetails() { System.out.println("Name: " + name); // Accessible (public) System.out.println("Salary: " + salary); // Accessible (protected) // System.out.println(healthID); // ERROR: private System.out.println("Days: " + vacationDays); // Accessible (same package) } } public class Main { public static void main(String[] args) { new Doctor().printDetails(); } }
Take Your Shot
Predicted Output

Type exactly what you think will be printed in the console.

12. Encapsulation: Access Modifiers in Action | DevExCode