Bird
0
0

What will be the output of the following Java code?

medium📝 Predict Output Q13 of 15
Java - Abstraction
What will be the output of the following Java code?
abstract class Animal {
    abstract void sound();
}

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
AAnimal sound
BBark
CCompilation error due to abstract method
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand class hierarchy and method implementation

    Class Animal is abstract with an abstract method sound(). Class Dog extends Animal and provides implementation for sound() that prints "Bark".
  2. Step 2: Analyze main method execution

    In main, an Animal reference points to a Dog object. Calling sound() invokes Dog's implementation, printing "Bark".
  3. Final Answer:

    Bark -> Option B
  4. Quick Check:

    Abstract method implemented in subclass = prints subclass output [OK]
Quick Trick: Abstract method calls subclass implementation at runtime [OK]
Common Mistakes:
  • Expecting abstract method to run without implementation
  • Confusing abstract class instantiation
  • Thinking abstract method prints default text

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes