Bird
0
0

What happens when a child class overrides a method from its parent class in PHP?

easy📝 Conceptual Q1 of 15
PHP - Inheritance and Polymorphism
What happens when a child class overrides a method from its parent class in PHP?
AThe parent class method is called instead of the child's method.
BThe child class provides its own version of the method, replacing the parent's version.
CBoth parent and child methods run automatically in sequence.
DThe method becomes private and cannot be accessed.
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding concept

    When a child class defines a method with the same name as in the parent, it replaces the parent's method.
  2. Step 2: Effect of overriding in PHP

    The child's method is called when invoked on the child object, hiding the parent's version.
  3. Final Answer:

    The child class provides its own version of the method, replacing the parent's version. -> Option B
  4. Quick Check:

    Method overriding = Child method replaces parent method [OK]
Quick Trick: Child method replaces parent method with same name [OK]
Common Mistakes:
  • Thinking both methods run automatically
  • Assuming parent method is called instead
  • Confusing overriding with overloading

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes