Challenge - 5 Problems
Method Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method overriding with parent call
What is the output of this PHP code when run?
PHP
<?php class Animal { public function sound() { return "Some sound"; } } class Dog extends Animal { public function sound() { return "Bark"; } } $dog = new Dog(); echo $dog->sound(); ?>
Attempts:
2 left
💡 Hint
Look at which class's method is called when you create a Dog object.
✗ Incorrect
The Dog class overrides the sound method of Animal. When calling $dog->sound(), the overridden method in Dog runs, returning "Bark".
❓ Predict Output
intermediate2:00remaining
Output when calling parent method inside overridden method
What will this PHP code output?
PHP
<?php class Vehicle { public function start() { return "Vehicle started"; } } class Car extends Vehicle { public function start() { return parent::start() . " and Car ready"; } } $car = new Car(); echo $car->start(); ?>
Attempts:
2 left
💡 Hint
The overridden method calls the parent method and adds extra text.
✗ Incorrect
The Car class overrides start() but calls parent::start() to get the base message, then adds " and Car ready". So the output is combined.
🔧 Debug
advanced2:00remaining
Identify the error in method overriding
What error will this PHP code produce when run?
PHP
<?php class Base { private function show() { echo "Base show"; } } class Child extends Base { public function show() { echo "Child show"; } } $obj = new Child(); $obj->show(); ?>
Attempts:
2 left
💡 Hint
Private methods are not visible to child classes, so Child defines its own method.
✗ Incorrect
The show method in Base is private, so Child does not override it but defines a new method. Calling $obj->show() calls Child's method, outputting "Child show".
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in method overriding?
Which of the following PHP code snippets will cause a syntax error?
Attempts:
2 left
💡 Hint
Check for missing braces or incorrect function syntax.
✗ Incorrect
Option B is missing the opening brace { after the method declaration, causing a syntax error.
🚀 Application
expert3:00remaining
Determine the final output with multiple overrides and parent calls
What is the output of this PHP code?
PHP
<?php class A { public function message() { return "A"; } } class B extends A { public function message() { return "B" . parent::message(); } } class C extends B { public function message() { return "C" . parent::message(); } } $obj = new C(); echo $obj->message(); ?>
Attempts:
2 left
💡 Hint
Each class adds its letter and calls the parent's message method.
✗ Incorrect
Class C calls B::message(), which calls A::message(). The output concatenates "C" + "B" + "A" = "CBA".