Bird
0
0

Identify the problem in this PHP code:

medium📝 Debug Q7 of 15
PHP - Inheritance and Polymorphism
Identify the problem in this PHP code:
class ParentClass {
  final public function display() {
    echo "Parent display";
  }
}
class ChildClass extends ParentClass {
  public function display() {
    echo "Child display";
  }
}
ACannot override a final method
BMissing parent::display() call
CMethod display() must be static
DNo problem, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Understand final keyword effect

    A method declared final cannot be overridden by child classes.
  2. Step 2: Check if child tries to override final method

    ChildClass tries to override display(), which is final in ParentClass, causing error.
  3. Final Answer:

    Cannot override a final method -> Option A
  4. Quick Check:

    final method cannot be overridden [OK]
Quick Trick: final methods cannot be overridden [OK]
Common Mistakes:
  • Ignoring final keyword
  • Thinking missing parent call causes error
  • Assuming static needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes