Complete the code to declare a final class named Vehicle.
<?php final class [1] { // class body } ?>
The keyword final is used before the class name to declare a final class. Here, the class name should be Vehicle.
Complete the code to declare a final method named startEngine inside a class.
<?php class Car { public [1] function startEngine() { echo "Engine started."; } } ?>
The keyword final before a method means it cannot be overridden in child classes.
Fix the error by completing the code to prevent overriding the method display in the subclass.
<?php class Base { public final function display() { echo "Base display."; } } class Derived extends Base { public function [1]() { echo "Derived display."; } } ?>
The method display is final in the base class and cannot be overridden. The subclass must use a different method name like show.
Fill both blanks to declare a final class and a final method inside it.
<?php [1] class Calculator { public [2] function add($a, $b) { return $a + $b; } } ?>
The class is declared final to prevent inheritance, and the method is declared final to prevent overriding.
Fill all three blanks to declare a final class, a final method, and call the method.
<?php [1] class Printer { public [2] function printText($text) { echo $text; } } $printer = new Printer(); $printer->[3]("Hello World!"); ?>
The class and method are declared final. The method printText is called on the object.