Challenge - 5 Problems
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of PHP inheritance example
What will be the output of the following PHP code demonstrating inheritance?
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 method is called when using the Dog object.
✗ Incorrect
The Dog class inherits from Animal but overrides the sound() method. So calling sound() on a Dog object outputs 'Bark'.
🧠 Conceptual
intermediate1:30remaining
Why use inheritance in PHP?
Why is inheritance needed in PHP programming?
Attempts:
2 left
💡 Hint
Think about how inheritance helps avoid repeating code.
✗ Incorrect
Inheritance allows a class to reuse code from another class and organize related classes in a hierarchy, making code easier to maintain and extend.
🔧 Debug
advanced2:00remaining
Identify the error in this inheritance code
What error will this PHP code produce?
PHP
<?php class Vehicle { public function start() { echo "Vehicle started"; } } class Car extends Vehicle { public function start() { echo "Car started"; } } $car = new Vehicle(); $car->start(); ?>
Attempts:
2 left
💡 Hint
Check which class is instantiated and which method is called.
✗ Incorrect
The object is created from Vehicle class, so calling start() outputs 'Vehicle started'. No error occurs.
📝 Syntax
advanced1:30remaining
Which code correctly defines inheritance in PHP?
Which of the following PHP code snippets correctly shows inheritance?
Attempts:
2 left
💡 Hint
Remember the keyword PHP uses for inheritance.
✗ Incorrect
PHP uses the keyword 'extends' to indicate inheritance between classes.
🚀 Application
expert2:30remaining
How many methods does the object have access to?
Given the following PHP code, how many methods can the $sportsCar object call?
PHP
<?php class Vehicle { public function start() { return "Vehicle started"; } public function stop() { return "Vehicle stopped"; } } class Car extends Vehicle { public function openDoor() { return "Door opened"; } } class SportsCar extends Car { public function turboBoost() { return "Turbo boost activated"; } } $sportsCar = new SportsCar(); ?>
Attempts:
2 left
💡 Hint
Count all methods from SportsCar and its parent classes accessible to the object.
✗ Incorrect
The $sportsCar object can call start(), stop() from Vehicle, openDoor() from Car, and turboBoost() from SportsCar, totaling 4 methods.