0
0
PHPprogramming~10 mins

Why inheritance is needed in PHP - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a class that inherits from another class.

PHP
<?php
class Animal {
    public function sound() {
        echo "Some sound";
    }
}

class Dog extends [1] {
}

$dog = new Dog();
$dog->sound();
?>
Drag options to blanks, or click blank then click option'
ADog
BAnimal
CSound
DPet
Attempts:
3 left
💡 Hint
Common Mistakes
Using the child class name instead of the parent class name after extends.
Forgetting to use the extends keyword.
2fill in blank
medium

Complete the code to override the method in the child class.

PHP
<?php
class Animal {
    public function sound() {
        echo "Some sound";
    }
}

class Cat extends Animal {
    public function [1]() {
        echo "Meow";
    }
}

$cat = new Cat();
$cat->sound();
?>
Drag options to blanks, or click blank then click option'
Asound
Bnoise
Cvoice
DmakeSound
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the method name so it does not override the parent method.
Forgetting to define the method in the child class.
3fill in blank
hard

Fix the error in the code to call the parent class method inside the child class method.

PHP
<?php
class Bird {
    public function sound() {
        echo "Chirp";
    }
}

class Parrot extends Bird {
    public function sound() {
        [1]::sound();
        echo " and Talk";
    }
}

$parrot = new Parrot();
$parrot->sound();
?>
Drag options to blanks, or click blank then click option'
ABird
Bself
Cparent
D$this
Attempts:
3 left
💡 Hint
Common Mistakes
Using self or class name instead of parent to call the parent method.
Using $this which calls the current class method causing recursion.
4fill in blank
hard

Fill both blanks to create a class that inherits and adds a new method.

PHP
<?php
class Vehicle {
    public function start() {
        echo "Starting vehicle";
    }
}

class Car [1] Vehicle {
    public function [2]() {
        echo "Driving car";
    }
}

$car = new Car();
$car->start();
$car->drive();
?>
Drag options to blanks, or click blank then click option'
Aextends
Bimplements
Cdrive
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using implements instead of extends for class inheritance.
Naming the new method incorrectly.
5fill in blank
hard

Fill all three blanks to create a class that inherits, overrides a method, and calls the parent method.

PHP
<?php
class Computer {
    public function boot() {
        echo "Booting computer";
    }
}

class Laptop [1] Computer {
    public function [2]() {
        [3]::boot();
        echo " with battery";
    }
}

$laptop = new Laptop();
$laptop->boot();
?>
Drag options to blanks, or click blank then click option'
Aextends
Bboot
Cparent
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keywords for inheritance.
Not matching the method name when overriding.
Calling the parent method incorrectly.