Bird
0
0

What will be the output of this PHP code?

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

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

$car = new Car();
echo $car->start();
AStarting
BFatal error: Cannot override final method
CCar starting
DParse error: syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Identify final method in parent class

    The method start() in Vehicle is declared final, so it cannot be overridden.
  2. Step 2: Check child class method override

    Car tries to override start(), which causes a fatal error.
  3. Final Answer:

    Fatal error: Cannot override final method -> Option B
  4. Quick Check:

    Overriding final method = fatal error [OK]
Quick Trick: Final methods cannot be overridden; override causes fatal error [OK]
Common Mistakes:
  • Expecting child method output
  • Ignoring final keyword on method
  • Confusing final method with abstract method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes