Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Inheritance and Polymorphism
What will be the output of this PHP code?
abstract class Vehicle {
  abstract public function start();
}

class Car extends Vehicle {
  public function start() {
    return "Car started";
  }
}

$car = new Car();
echo $car->start();
AFatal error: Cannot instantiate abstract class Vehicle
BNothing is printed
CCar started
DError: start() method not implemented
Step-by-Step Solution
Solution:
  1. Step 1: Analyze class inheritance and method implementation

    Class Car extends abstract Vehicle and implements the abstract method start().
  2. Step 2: Check object creation and method call

    Car object is created and start() method returns "Car started" which is echoed.
  3. Final Answer:

    Car started -> Option C
  4. Quick Check:

    Implemented abstract method call = "Car started" [OK]
Quick Trick: Child must implement abstract methods to avoid errors [OK]
Common Mistakes:
  • Trying to instantiate abstract class directly
  • Forgetting to implement abstract method
  • Expecting no output or error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes