0
0
PHPprogramming~20 mins

Final classes and methods in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Final Mastery in PHP
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of final method call in subclass
What is the output of this PHP code?
PHP
<?php
class Base {
    final public function greet() {
        echo "Hello from Base";
    }
}

class Child extends Base {
    public function greet() {
        echo "Hello from Child";
    }
}

$child = new Child();
$child->greet();
?>
AFatal error: Cannot override final method Base::greet()
BHello from Child
CHello from Base
DNo output
Attempts:
2 left
💡 Hint
Remember that final methods cannot be overridden in subclasses.
Predict Output
intermediate
2:00remaining
Output when instantiating a final class
What will this PHP code output?
PHP
<?php
final class FinalClass {
    public function sayHi() {
        return "Hi!";
    }
}

class SubClass extends FinalClass {
}

$obj = new SubClass();
echo $obj->sayHi();
?>
AFatal error: Class SubClass may not inherit from final class (FinalClass)
BHi!
CParse error: syntax error
DNo output
Attempts:
2 left
💡 Hint
Final classes cannot be extended.
🔧 Debug
advanced
2:00remaining
Identify the error in final method redeclaration
This PHP code causes an error. What is the error message?
PHP
<?php
class ParentClass {
    final public function doSomething() {
        echo "Doing something";
    }
}

class ChildClass extends ParentClass {
    public function doSomething() {
        echo "Doing something else";
    }
}
?>
ANo error, code runs fine
BFatal error: Cannot override final method ParentClass::doSomething()
CParse error: syntax error, unexpected 'public' (T_PUBLIC)
DWarning: Method ChildClass::doSomething() should be compatible with ParentClass::doSomething()
Attempts:
2 left
💡 Hint
Final methods cannot be overridden in child classes.
🧠 Conceptual
advanced
1:30remaining
Why use final classes or methods?
Which of the following is the best reason to declare a class or method as final in PHP?
ATo make the class or method private and inaccessible outside the class
BTo allow multiple inheritance from the final class
CTo prevent the class or method from being extended or overridden, ensuring consistent behavior
DTo automatically optimize the code for faster execution
Attempts:
2 left
💡 Hint
Think about controlling how code can be changed by others.
Predict Output
expert
2:30remaining
Output of final method called via parent reference
What is the output of this PHP code?
PHP
<?php
class A {
    final public function show() {
        echo "A";
    }
}

class B extends A {
    public function show() {
        echo "B";
    }
}

function callShow(A $obj) {
    $obj->show();
}

$b = new B();
callShow($b);
?>
AType error: Argument 1 passed to callShow() must be of type A, B given
BB
CA
DFatal error: Cannot override final method A::show()
Attempts:
2 left
💡 Hint
Check if class B can override the final method show() from class A.