Java - What is the output: instanceof and inheritance-chain

Question

What is the output from the following code


class Employee {
  private String name = "Unknown";

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}

class Manager extends Employee {
  public void giveOrder(){
    System.out.println("order");
  }
}
class Clerk{
  
}
public class Main {
  public static void main(String[] args) {
    Manager mgr = new Manager();
    if (mgr instanceof Clerk) {
      System.out.println("clerk");
    }

  }
}


Click to view the answer

if (mgr instanceof Clerk) { //  A compile-time error

Note

The use of the instanceof operator will generate a compiler error because the Clerk class is not in the inheritance-chain of the Manager class.

Related Quiz