Skip to content
CodeKruPro

Parent reference, child object: what runs?

QuizMediumJava onlyAsked in: Accenture, Capgemini, Cognizant, Infosys, TCS, Wipro

What does the last line print?

class Animal {
    String name = "animal";
    static String kind() { return "Animal.kind"; }
    String sound() { return "..."; }
}
class Dog extends Animal {
    String name = "dog";
    static String kind() { return "Dog.kind"; }
    String sound() { return "woof"; }
}

// in main:
Animal x = new Dog();
System.out.println(x.name + " " + x.sound() + " " + x.kind());
  • animal woof Animal.kind
  • dog woof Dog.kind
  • animal ... Animal.kind
  • dog woof Animal.kind

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in