Bird
0
0

Identify the error in this PHP code using inheritance:

medium📝 Debug Q14 of 15
PHP - Inheritance and Polymorphism
Identify the error in this PHP code using inheritance:
class ParentClass {
  public function greet() {
    return "Hello";
  }
}
class ChildClass ParentClass {
  public function greet() {
    return "Hi";
  }
}
AMethod greet() cannot be overridden
BMissing 'extends' keyword before ParentClass
CClass names cannot be ParentClass or ChildClass
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check inheritance syntax

    In PHP, to inherit, the child class must use the keyword extends before the parent class name.
  2. Step 2: Identify missing keyword

    The code misses extends in class ChildClass ParentClass, causing a syntax error.
  3. Final Answer:

    Missing 'extends' keyword before ParentClass -> Option B
  4. Quick Check:

    Inheritance requires 'extends' keyword [OK]
Quick Trick: Inheritance needs 'extends' keyword in class declaration [OK]
Common Mistakes:
  • Omitting 'extends' keyword
  • Thinking method override is disallowed
  • Assuming class names cause error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes