Bird
0
0

Given these classes:

hard📝 Application Q9 of 15
PHP - Inheritance and Polymorphism
Given these classes:
class A {
    public function foo() {
        return "A";
    }
}
class B extends A {
    public function foo() {
        return "B" . parent::foo();
    }
}
class C extends B {
    public function foo() {
        return "C" . parent::foo();
    }
}
$obj = new C();
echo $obj->foo();

What is the output?
ACB
BCBA
CABC
DCBAA
Step-by-Step Solution
Solution:
  1. Step 1: Trace method calls from class C

    Calling foo() on C returns "C" plus parent::foo() which is B's foo.
  2. Step 2: Trace B's foo method

    B's foo returns "B" plus parent::foo() which is A's foo returning "A".
  3. Step 3: Combine results

    So output is "C" + "B" + "A" = "CBA".
  4. Final Answer:

    CBA -> Option B
  5. Quick Check:

    Chained parent calls concatenate strings [OK]
Quick Trick: parent:: calls chain up inheritance tree [OK]
Common Mistakes:
  • Stopping at first parent call
  • Mixing order of concatenation
  • Ignoring parent:: calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes