Bird
0
0

Given the following classes, what will be the output when creating a new Child object?

hard📝 Application Q15 of 15
PHP - Inheritance and Polymorphism
Given the following classes, what will be the output when creating a new Child object?
class Parent {
  public function __construct() {
    echo "Parent start. ";
  }
}
class Child extends Parent {
  public function __construct() {
    echo "Child start. ";
    parent::__construct();
    echo "Child end.";
  }
}
new Child();
AParent start. Child end.
BParent start. Child start. Child end.
CChild start. Child end.
DChild start. Parent start. Child end.
Step-by-Step Solution
Solution:
  1. Step 1: Follow the order of echo statements

    The child constructor first prints "Child start. ". Then it calls parent::__construct(); which prints "Parent start. ". Finally, the child prints "Child end.".
  2. Step 2: Combine outputs in sequence

    Putting all together, the output is "Child start. Parent start. Child end." exactly in that order.
  3. Final Answer:

    Child start. Parent start. Child end. -> Option D
  4. Quick Check:

    Child prints before and after parent constructor call [OK]
Quick Trick: Order matters: child's code runs before and after parent::__construct() [OK]
Common Mistakes:
  • Assuming parent constructor runs before child's code
  • Ignoring order of echo statements
  • Thinking parent's constructor output appears last

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes