What is an abstract class in PHP?
A
A class that cannot be instantiated directly
B
A class without methods
C
A class with only static methods
D
A class without properties
Analysis & Theory
An abstract class cannot be instantiated directly; it must be extended by another class.
Which keyword is used to declare an abstract class in PHP?
Analysis & Theory
The `abstract` keyword is used to declare an abstract class in PHP.
What is true about abstract methods?
A
They have no body in the abstract class and must be implemented by child classes
B
They must have a body in the abstract class
C
They are optional to implement in child classes
D
They can only be private
Analysis & Theory
Abstract methods are declared without a body and must be implemented by any subclass.
Can an abstract class have non-abstract (regular) methods?
A
No, it can only have abstract methods
B
Yes, it can have both abstract and regular methods
C
No, only static methods are allowed
D
Yes, but only if protected
Analysis & Theory
An abstract class can have both abstract methods and regular methods with functionality.
What happens if a child class does not implement an abstract method?
B
The child becomes abstract
C
It runs without any issue
Analysis & Theory
If a child class does not implement all abstract methods, it must be declared as abstract itself.
Can an abstract class be extended by multiple classes?
Analysis & Theory
Yes, an abstract class can be extended by multiple child classes.
What is the output?
```
abstract class Animal {
abstract public function sound();
}
class Dog extends Animal {
public function sound() {
echo "Bark";
}
}
$d = new Dog();
$d->sound();
```
Analysis & Theory
The output is `Bark` because the Dog class implements the abstract method `sound()`.
Can you create an object directly from an abstract class?
C
Only if it has no abstract methods
D
Only if it has a constructor
Analysis & Theory
No, abstract classes cannot be instantiated directly.
Which of the following is a correct syntax for declaring an abstract method?
A
public function run() { }
B
abstract public function run();
D
function abstract run();
Analysis & Theory
The correct syntax is `abstract public function run();` without a body.
Why would you use an abstract class?
A
To create objects directly
B
To define a blueprint for other classes to implement common methods
C
To make all methods static
D
To avoid using inheritance
Analysis & Theory
Abstract classes provide a template with method declarations that must be implemented by derived classes.