0
0
PHPprogramming~10 mins

Final classes and methods in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a final class named Vehicle.

PHP
<?php
final class [1] {
    // class body
}
?>
Drag options to blanks, or click blank then click option'
AClass
Bfinal
CCar
DVehicle
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the keyword 'final' as the class name.
Using lowercase 'class' as the class name.
Forgetting to write the class name.
2fill in blank
medium

Complete the code to declare a final method named startEngine inside a class.

PHP
<?php
class Car {
    public [1] function startEngine() {
        echo "Engine started.";
    }
}
?>
Drag options to blanks, or click blank then click option'
Astatic
Bfinal
Cabstract
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' instead of 'final'.
Using 'abstract' which is for methods without body.
Using 'private' which controls visibility, not overriding.
3fill in blank
hard

Fix the error by completing the code to prevent overriding the method display in the subclass.

PHP
<?php
class Base {
    public final function display() {
        echo "Base display.";
    }
}

class Derived extends Base {
    public function [1]() {
        echo "Derived display.";
    }
}
?>
Drag options to blanks, or click blank then click option'
Ashow
Bdisplay
CdisplayBase
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to override the final method with the same name.
Using a method name that already exists as final in the parent.
4fill in blank
hard

Fill both blanks to declare a final class and a final method inside it.

PHP
<?php
[1] class Calculator {
    public [2] function add($a, $b) {
        return $a + $b;
    }
}
?>
Drag options to blanks, or click blank then click option'
Afinal
Bstatic
Dabstract
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' or 'abstract' instead of 'final'.
Forgetting to put 'final' before the method.
5fill in blank
hard

Fill all three blanks to declare a final class, a final method, and call the method.

PHP
<?php
[1] class Printer {
    public [2] function printText($text) {
        echo $text;
    }
}

$printer = new Printer();
$printer->[3]("Hello World!");
?>
Drag options to blanks, or click blank then click option'
Afinal
CprintText
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' instead of 'final'.
Calling a method name that does not exist.