Bird
0
0

You want to create a class Square that extends Rectangle and automatically sets equal width and height. Which approach is correct?

hard📝 Application Q8 of 15
PHP - Inheritance and Polymorphism
You want to create a class Square that extends Rectangle and automatically sets equal width and height. Which approach is correct?
AOverride constructor in Square to call parent constructor with same width and height
BDo not define constructor in Square; use Rectangle's constructor directly
CDefine separate width and height properties in Square without calling parent
DUse PHP trait instead of extending Rectangle
Step-by-Step Solution
Solution:
  1. Step 1: Understand constructor inheritance

    To set width and height equal, Square must override constructor and call parent's constructor with same values.
  2. Step 2: Evaluate options

    Override constructor in Square to call parent constructor with same width and height correctly overrides constructor and calls parent with equal sides.
  3. Final Answer:

    Override constructor in Square to call parent constructor with same width and height -> Option A
  4. Quick Check:

    Override constructor and call parent with parameters [OK]
Quick Trick: Override constructor and call parent::__construct() with needed values [OK]
Common Mistakes:
  • Not calling parent constructor
  • Defining properties without parent initialization
  • Using traits instead of inheritance incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes