0
0
PHPprogramming~20 mins

Method overriding in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Method Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
?>
AError: Cannot override method
BSome sound
CBark
DNo output
Attempts:
2 left
💡 Hint
Look at which class's method is called when you create a Dog object.
Predict Output
intermediate
2: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();
?>
AFatal error: Cannot access parent method
BCar ready
CVehicle started
DVehicle started and Car ready
Attempts:
2 left
💡 Hint
The overridden method calls the parent method and adds extra text.
🔧 Debug
advanced
2: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();
?>
AOutputs: Child show
BOutputs: Base show
CFatal error: Cannot override private method Base::show()
DFatal error: Call to private method Base::show() from Child
Attempts:
2 left
💡 Hint
Private methods are not visible to child classes, so Child defines its own method.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in method overriding?
Which of the following PHP code snippets will cause a syntax error?
A
&lt;?php
class A {
    public function test() {}
}
class B extends A {
    public function test() {
        parent::test();
        echo "Hi";
    }
}
?&gt;
B
&lt;?php
class A {
    public function test() {}
}
class B extends A {
    public function test() 
        echo "Hi";
    }
}
?&gt;
C
&lt;?php
class A {
    public function test() {}
}
class B extends A {
    public function test() {
        echo "Hello";
    }
}
?&gt;
D
&lt;?php
class A {
    public function test() {}
}
class B extends A {
    public function test() {
        return "Hi";
    }
}
?&gt;
Attempts:
2 left
💡 Hint
Check for missing braces or incorrect function syntax.
🚀 Application
expert
3: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();
?>
ACBA
BError: Cannot call parent method
CCAB
DABC
Attempts:
2 left
💡 Hint
Each class adds its letter and calls the parent's message method.