0
0
PHPprogramming~20 mins

Abstract classes and methods in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Abstract Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of abstract class method call

What is the output of this PHP code?

PHP
<?php
abstract class Animal {
    abstract public function sound();

    public function describe() {
        return "This animal says: " . $this->sound();
    }
}

class Dog extends Animal {
    public function sound() {
        return "Woof";
    }
}

$dog = new Dog();
echo $dog->describe();
?>
AThis animal says: sound
BFatal error: Cannot instantiate abstract class Animal
CWoof
DThis animal says: Woof
Attempts:
2 left
💡 Hint

Look at how the describe method uses the abstract sound method implemented in the subclass.

Predict Output
intermediate
2:00remaining
What error occurs when instantiating an abstract class?

What happens when you run this PHP code?

PHP
<?php
abstract class Vehicle {
    abstract public function move();
}

$car = new Vehicle();
?>
ANo output, code runs fine
BFatal error: Cannot instantiate abstract class Vehicle
CParse error: syntax error, unexpected 'abstract'
DWarning: Abstract method move() not implemented
Attempts:
2 left
💡 Hint

Remember, abstract classes cannot be directly created as objects.

🔧 Debug
advanced
2:00remaining
Identify the error in abstract method implementation

What error will this PHP code produce?

PHP
<?php
abstract class Shape {
    abstract public function area();
}

class Circle extends Shape {
    public function area($radius) {
        return 3.14 * $radius * $radius;
    }
}

$circle = new Circle();
echo $circle->area(5);
?>
AFatal error: Declaration of Circle::area($radius) must be compatible with Shape::area()
BOutput: 78.5
CParse error: syntax error, unexpected '$radius'
DWarning: Missing argument for area()
Attempts:
2 left
💡 Hint

Check if the method signatures match exactly between abstract and subclass methods.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly defines an abstract class with an abstract method?

Choose the correct PHP code that defines an abstract class with one abstract method run.

A
&lt;?php
abstract class Runner {
    abstract public function run();
}
?&gt;
B
&lt;?php
class Runner {
    abstract public function run();
}
?&gt;
C
&lt;?php
abstract class Runner {
    public abstract function run() {}
}
?&gt;
D
&lt;?php
abstract class Runner {
    abstract function run() {}
}
?&gt;
Attempts:
2 left
💡 Hint

Remember abstract methods cannot have a body and must be inside an abstract class.

🚀 Application
expert
2:00remaining
How many methods must be implemented?

Given this PHP code, how many methods must the class Smartphone implement to avoid errors?

PHP
<?php
abstract class Device {
    abstract public function powerOn();
    abstract public function powerOff();
    public function reset() {
        $this->powerOff();
        $this->powerOn();
    }
}

abstract class Phone extends Device {
    abstract public function call($number);
}

class Smartphone extends Phone {
    public function powerOn() {
        echo "Powering on";
    }

    public function powerOff() {
        echo "Powering off";
    }

    // Missing call method implementation
}
?>
A3
B2
C1
D0
Attempts:
2 left
💡 Hint

Count all abstract methods from parent classes that are not yet implemented.