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?
class Vehicle {
  public function start() {
    return "Vehicle started";
  }
}
class Car extends Vehicle {
  public function start() {
    return "Car started";
  }
}
$car = new Car();
echo $car->start();
ACar started
BNo output
CError: Method start() not found
DVehicle started
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding in inheritance

    The Car class overrides the start() method of Vehicle.
  2. Step 2: Determine which method is called

    When calling $car->start(), the overridden method in Car runs, returning "Car started".
  3. Final Answer:

    Car started -> Option A
  4. Quick Check:

    Overridden method output = Car started [OK]
Quick Trick: Child class method overrides parent method if same name used [OK]
Common Mistakes:
  • Expecting parent method output instead of child
  • Thinking method not found error occurs
  • Assuming no output without echo

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes