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


What is the output from the following code

class Employee {
  private String name = "Unknown";

  public void setName(String name) { = name;

  public String getName() {
    return name;

class Manager extends Employee {
  public void giveOrder(){
class Clerk{
public class Main {
  public static void main(String[] args) {
    Manager mgr = new Manager();
    if (mgr instanceof Clerk) {


Click to view the answer

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


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