A developer writes a class 'Vehicle' with a method 'move()' that prints 'Vehicle moves'. A subclass 'Car' overrides 'move()' to print 'Car moves'. Given: Vehicle v = new Car(); v.move(); What is the output?
Trap 1: Vehicle moves
Static binding would give this, but method overriding uses dynamic binding.
Trap 2: Runtime exception
No exception; method invocation is valid.
Trap 3: Compilation fails
The code compiles fine; Car is a subclass of Vehicle.
- A
Vehicle moves
Why wrong: Static binding would give this, but method overriding uses dynamic binding.
- B
Runtime exception
Why wrong: No exception; method invocation is valid.
- C
Compilation fails
Why wrong: The code compiles fine; Car is a subclass of Vehicle.
- D
Car moves
Correct due to polymorphism; the overridden method in Car is called.