1Z0-809 · Question #148
Given the following classes: public class Employee { public int salary; } public class Manager extends Employee { public int budget; } public class Director extends Manager { public int…
The correct answer is A. manager .stockoption = 500. A and D are the two options that fail to compile, because they attempt to access fields that don't exist on the declared type of the variable. A fails: manager is declared as type Manager, and Manager only inherits salary from Employee and defines budget - stockoptions belongs…
Question
Options
- Amanager .stockoption = 500;
- Bdirector .stockoption = 1_000;
- Cdirector .salary = 50_000;
- Demployee .budget = 200_000;
- Eemployee .salary = 50_000;
- Fmanager .budget = 1_000_000;
How the community answered
(21 responses)- A71% (15)
- C10% (2)
- E14% (3)
- F5% (1)
Explanation
A and D are the two options that fail to compile, because they attempt to access fields that don't exist on the declared type of the variable.
- A fails:
manageris declared as typeManager, andManageronly inheritssalaryfromEmployeeand definesbudget-stockoptionsbelongs toDirector(a subclass), and a parent type cannot access a child's members. - D fails:
employeeis declared as typeEmployee, which only hassalary-budgetis defined inManager(a subclass), so it's inaccessible via anEmployeereference. - B passes:
directoris declared asDirector, which does definestockoptions- valid access. - C passes:
directorinheritssalaryall the way up fromEmployee- subclasses inherit all non-private members. - E passes:
employee.salarydirectly accesses its own field. - F passes:
manager.budgetaccessesManager's own field.
Memory tip: Inheritance flows down - a variable can only see fields at its own level and above (ancestors), never below (descendants). Think of it as "a Manager knows about Employee things, but not Director things."
Community Discussion
No community discussion yet for this question.