Bird
0
0

What will this PHP code output?

medium📝 Predict Output Q5 of 15
PHP - Inheritance and Polymorphism
What will this PHP code output?
class ParentClass {
  public $name = "Parent";
}
class ChildClass extends ParentClass {
  public $name = "Child";
}
$obj = new ChildClass();
echo $obj->name;
AChild
BParent
CError: Cannot access property
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Check property overriding in inheritance

    The ChildClass defines its own $name property, overriding the parent's.
  2. Step 2: Identify which property is accessed

    Accessing $obj->name returns the child's property value "Child".
  3. Final Answer:

    Child -> Option A
  4. Quick Check:

    Property override returns child's value [OK]
Quick Trick: Child class property overrides parent property [OK]
Common Mistakes:
  • Expecting parent's property value
  • Thinking property access causes error
  • Assuming no output without echo

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes