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 stockOptions;
}

And given the follow main method:

public static void main(String[] args){
    Employee employee = new Employee();
    Manager manager = new Manager();
    Director director = new Director();
    // line n1
}

Which two options fail to compile when placed at line n1 of the main method?

A.

employee.salary = 50_000;
B.
director.salary = 80_000;
C.
employee.budget = 200_000;
D.
manager.budget = 1_000_000;
E.
manager.stockOption = 500;
F.
director.stockOptions = 1_000;

題解

選項 A,Employee 物件本身有 salary 欄位,因此不會有問題。

選項 B,Director 物件也是 Employee,有 salary 欄位,因此也不會有問題。

選項 C,Employee 物件並沒有 budget 欄位,編譯會有問題。

選項 D,Manager 物件本身有 budget 欄位,因此不會有問題。

選項 E,Manager 物件並沒有 stockOption 欄位,編譯會有問題。

選項 F,Director 物件本身有 stockOptions 欄位,因此不會有問題。