Bird
0
0

Consider the following PHP classes:

hard📝 Application Q9 of 15
PHP - Inheritance and Polymorphism
Consider the following PHP classes:
class Vehicle {
  function start() {
    echo "Starting vehicle";
  }
}
class Car extends Vehicle {
  function start() {
    echo "Starting car";
  }
}
class ElectricCar extends Car {
  function start() {
    parent::start();
    echo " silently";
  }
}

What will be the output of:
$ec = new ElectricCar();
$ec->start();
AStarting carStarting vehicle silently
BStarting vehicle silently
CStarting vehicleStarting car silently
DStarting car silently
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method calls

    ElectricCar's start() calls parent::start(), which refers to Car's start() method.
  2. Step 2: Output sequence

    Car's start() echoes "Starting car", then ElectricCar appends " silently".
  3. Final Answer:

    Starting car silently -> Option D
  4. Quick Check:

    parent:: calls immediate parent's method [OK]
Quick Trick: parent:: calls immediate parent's method [OK]
Common Mistakes:
  • Assuming parent:: calls grandparent's method
  • Concatenating outputs without spaces
  • Confusing method resolution order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes