Bird
0
0

Identify the error in this PHP code:

medium📝 Debug Q14 of 15
PHP - Interfaces and Traits
Identify the error in this PHP code:
interface Shape {
  public function area();
}

class Circle implements Shape {
  public function area() {
    return 3.14 * 5 * 5;
  }

  public function perimeter() {
    return 2 * 3.14 * 5;
  }
}

$circle = new Circle();
echo $circle->area();
ANo error, code runs and outputs area
BMissing implementation of perimeter() in interface
CInterface method area() has no body, causing error
DClass Circle must implement all interface methods
Step-by-Step Solution
Solution:
  1. Step 1: Check interface and class methods

    Interface Shape declares area(). Circle implements area() correctly and adds extra method perimeter() which is allowed.
  2. Step 2: Verify if code runs without error

    Since all interface methods are implemented, extra methods are allowed. Code outputs area value.
  3. Final Answer:

    No error, code runs and outputs area -> Option A
  4. Quick Check:

    Extra methods allowed, interface methods implemented [OK]
Quick Trick: Extra methods in class are allowed beyond interface [OK]
Common Mistakes:
  • Thinking extra methods must be in interface
  • Confusing method body rules in interface
  • Assuming missing interface methods cause error here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes