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物件本身有stockOption欄位,因此不會有問題。