Recall & Review
beginner
What is an abstract class in PHP?An abstract class in PHP is a class that cannot be instantiated on its own. It is meant to be a base class that other classes extend. It can contain abstract methods that must be implemented by child classes.Click to reveal answer
beginner
What is an abstract method?
An abstract method is a method declared in an abstract class without a body. Child classes must provide their own implementation of this method.Click to reveal answer
beginner
Can you create an object from an abstract class directly?No, you cannot create an object directly from an abstract class. You must extend it with a child class and instantiate the child class instead.Click to reveal answer
intermediate
Why use abstract classes and methods?
Abstract classes and methods help define a common template for related classes. They enforce that certain methods are implemented in child classes, ensuring consistent behavior.
Click to reveal answer
beginner
Example of declaring an abstract class with an abstract method in PHP?Example:<br><pre>abstract class Animal {
abstract public function makeSound();
}
class Dog extends Animal {
public function makeSound() {
echo "Woof!";
}
}</pre>Click to reveal answer
Which keyword is used to declare an abstract class in PHP?
✗ Incorrect
The keyword abstract is used to declare an abstract class in PHP.
What happens if a child class does not implement an abstract method from its parent abstract class?
✗ Incorrect
PHP throws a fatal error if a child class does not implement all abstract methods from its parent abstract class.
Can an abstract class contain non-abstract methods?
✗ Incorrect
An abstract class can have both abstract methods (without body) and non-abstract methods (with body).
Which of these is true about abstract classes?
✗ Incorrect
Abstract classes can implement interfaces. They cannot be instantiated directly, can have properties, and do not have to have abstract methods.
How do you declare an abstract method in PHP?
✗ Incorrect
Abstract methods are declared with the keyword abstract and a visibility modifier like public and have no body, e.g.,
abstract public function methodName();.Explain what an abstract class is and why you would use one in PHP.
Think about a blueprint that sets rules for other classes.
You got /4 concepts.
Describe how abstract methods work and what happens if a child class does not implement them.
Consider a promise that child classes must keep.
You got /3 concepts.