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();
    }
}
ABark
BAnimal sound
CCompilation error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand abstract class and method implementation

    The abstract class Animal has an abstract method sound(). Dog class extends Animal and provides implementation for sound() method.
  2. Step 2: Analyze the main method execution

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

    Bark -> Option A
  4. Quick Check:

    Abstract method implemented = "Bark" output [OK]
Quick Trick: Abstract method must be implemented to run [OK]
Common Mistakes:
  • Expecting abstract class to be instantiated
  • Thinking abstract method runs without implementation
  • Confusing compile and runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes