Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
Java - Abstraction

What will be the output of the following code?

abstract class Vehicle {
    abstract void start();
    void stop() {
        System.out.println("Vehicle stopped");
    }
}
class Car extends Vehicle {
    void start() {
        System.out.println("Car started");
    }
}
public class Test {
    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();
        v.stop();
    }
}
ACar started Car stopped
BVehicle started Vehicle stopped
CCar started Vehicle stopped
DCompilation error due to abstract method
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method calls on Vehicle reference

    Vehicle reference points to Car object. start() is overridden in Car, so 'Car started' prints.
  2. Step 2: Check stop() method call

    stop() is concrete in Vehicle, so 'Vehicle stopped' prints.
  3. Final Answer:

    Car started Vehicle stopped -> Option C
  4. Quick Check:

    Abstract method overridden, concrete method inherited [OK]
Quick Trick: Abstract methods must be overridden; concrete methods inherited [OK]
Common Mistakes:
  • Expecting stop() to print 'Car stopped'
  • Thinking abstract method causes compile error here
  • Confusing reference type and object type method calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes