Bird
Raised Fist0
PHPprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the final keyword do when used with a class in PHP?
easy
A. Prevents the class from being extended by other classes
B. Allows the class to be extended multiple times
C. Makes the class abstract
D. Allows the class methods to be overridden

Solution

  1. Step 1: Understand the role of final with classes

    The final keyword before a class means no other class can extend it.
  2. Step 2: Compare with other options

    Options A, B, and D describe behaviors that are opposite or unrelated to final classes.
  3. Final Answer:

    Prevents the class from being extended by other classes -> Option A
  4. Quick Check:

    final class = no extension allowed [OK]
Hint: Final class means no child classes allowed [OK]
Common Mistakes:
  • Thinking final class can be extended
  • Confusing final with abstract
  • Assuming final allows method overrides
2. Which of the following is the correct syntax to declare a final method in a PHP class?
easy
A. function final myMethod() {}
B. final function myMethod() {}
C. function myMethod() final {}
D. final myMethod function() {}

Solution

  1. Step 1: Recall correct final method syntax

    In PHP, the final keyword comes before the function keyword when declaring a final method.
  2. Step 2: Check each option

    final function myMethod() {} matches the correct syntax. Options A, C, and D have incorrect order or placement of keywords.
  3. Final Answer:

    final function myMethod() {} -> Option B
  4. Quick Check:

    final before function keyword = correct syntax [OK]
Hint: final keyword always before function name in declaration [OK]
Common Mistakes:
  • Placing final after function name
  • Writing final after function keyword incorrectly
  • Mixing order of keywords
3. What will be the output of the following PHP code?
final class Car {
    final public function start() {
        return "Engine started";
    }
}

class SportsCar extends Car {
    public function start() {
        return "Sports engine started";
    }
}

$car = new SportsCar();
echo $car->start();
medium
A. Engine started
B. Sports engine started
C. Fatal error: Cannot override final method Car::start()
D. Fatal error: Cannot extend final class Car

Solution

  1. Step 1: Analyze class and method declarations

    The class Car is declared as final, so it cannot be extended. But the code tries to extend it with SportsCar.
  2. Step 2: Identify the error

    Since Car is final, extending it causes a fatal error. But the question is about overriding a final method. Actually, the class is final, so extending is not allowed at all.
  3. Step 3: Check options carefully

    Fatal error: Cannot extend final class Car says cannot extend final class, which matches the error. Fatal error: Cannot override final method Car::start() says cannot override final method, which would be error if class was not final but method was final.
  4. Final Answer:

    Fatal error: Cannot extend final class Car -> Option D
  5. Quick Check:

    final class cannot be extended = fatal error [OK]
Hint: Final class cannot be extended, causes fatal error [OK]
Common Mistakes:
  • Confusing final method error with final class error
  • Assuming method override works despite final class
  • Ignoring that class is final and cannot be extended
4. Identify the error in the following PHP code:
class Vehicle {
    final public function move() {
        return "Moving";
    }
}

class Bike extends Vehicle {
    public function move() {
        return "Bike moving";
    }
}
medium
A. No error, code runs fine
B. Cannot extend class Vehicle
C. Cannot override final method move() in class Bike
D. Syntax error in method declaration

Solution

  1. Step 1: Understand final method behavior

    The method move() in Vehicle is declared as final, so child classes cannot override it.
  2. Step 2: Check the child class method

    The Bike class tries to override move(), which is not allowed and causes a fatal error.
  3. Final Answer:

    Cannot override final method move() in class Bike -> Option C
  4. Quick Check:

    final method cannot be overridden [OK]
Hint: Final method cannot be changed in child classes [OK]
Common Mistakes:
  • Thinking final method can be overridden
  • Confusing final method with abstract method
  • Assuming no error when overriding final method
5. You want to create a base class Document that cannot be extended, but you want to allow extending a method print() in child classes. Which is the correct approach?
hard
A. Do not declare class Document as final, and declare method print() normally
B. Declare class Document as final, and method print() normally (not final)
C. Do not declare class Document as final, but declare method print() as final
D. Declare class Document as final, and method print() as final

Solution

  1. Step 1: Understand final class effect

    Declaring a class as final prevents any extension, so no child classes can exist to override methods.
  2. Step 2: Understand method overriding

    To allow overriding print(), the class must be extendable and the method must not be final.
  3. Step 3: Choose correct option

    Do not declare class Document as final, and declare method print() normally allows extending the class and overriding the method. Options B and D prevent extension. Do not declare class Document as final, but declare method print() as final prevents method overriding.
  4. Final Answer:

    Do not declare class Document as final, and declare method print() normally -> Option A
  5. Quick Check:

    Allow extension and method override = no final on class or method [OK]
Hint: Final class blocks extension; omit final to allow overrides [OK]
Common Mistakes:
  • Declaring class final but expecting method override
  • Declaring method final when override is needed
  • Confusing final class and final method effects
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard