📖 Scenario: Imagine you are building a simple system for different types of vehicles. Each vehicle has a way to start the engine, but the exact way depends on the vehicle type.
🎯 Goal: You will create an abstract class called Vehicle with an abstract method startEngine(). Then, you will create two classes Car and Motorcycle that extend Vehicle and provide their own way to start the engine. Finally, you will create objects of these classes and call their startEngine() methods to see the output.
📋 What You'll Learn
Create an abstract class Vehicle with an abstract method startEngine().
Create a class Car that extends Vehicle and implements startEngine() to print "Car engine started."
Create a class Motorcycle that extends Vehicle and implements startEngine() to print "Motorcycle engine started."
Create objects of Car and Motorcycle and call their startEngine() methods.
Print the output exactly as specified.
💡 Why This Matters
🌍 Real World
Abstract classes are used in software design to create a base template for related objects, ensuring they share common methods but can have different behaviors. For example, different vehicle types share the concept of starting an engine but do it differently.
💼 Career
Understanding abstract classes and methods is important for writing clean, organized, and reusable code in object-oriented programming, which is a key skill in many software development jobs.
Progress0 / 4 steps
1
Create the abstract class Vehicle
Create an abstract class called Vehicle with an abstract method startEngine().
PHP
Hint
Use the abstract keyword before the class and method. The method has no body.
2
Create Car and Motorcycle classes
Create a class called Car that extends Vehicle and implements the startEngine() method to print "Car engine started.". Also create a class called Motorcycle that extends Vehicle and implements the startEngine() method to print "Motorcycle engine started.".
PHP
Hint
Remember to use extends Vehicle and implement the startEngine() method with echo statements.
3
Create objects of Car and Motorcycle
Create an object called $car of class Car and an object called $motorcycle of class Motorcycle.
PHP
Hint
Use the new keyword to create objects.
4
Call startEngine() methods and print output
Call the startEngine() method on the $car object and then on the $motorcycle object. Print each output on a new line.
PHP
Hint
Use echo "\n"; to print a new line between outputs.
Practice
(1/5)
1. What is the main purpose of an abstract class in PHP?
easy
A. To store data without any methods
B. To create objects directly without any child classes
C. To allow multiple inheritance in PHP
D. To provide a base class that cannot be instantiated directly
Solution
Step 1: Understand abstract class concept
An abstract class is designed to be a base class that other classes extend but cannot be instantiated on its own.
Step 2: Eliminate incorrect options
Options A, B, and D do not describe abstract classes correctly. PHP does not support multiple inheritance, and abstract classes cannot be instantiated directly.
Final Answer:
To provide a base class that cannot be instantiated directly -> Option D
Quick Check:
Abstract class = base class no direct objects [OK]
2. Which of the following is the correct way to declare an abstract method in a PHP abstract class?
easy
A. abstract public function myMethod();
B. public function myMethod() {}
C. function abstract myMethod();
D. public abstract myMethod() {}
Solution
Step 1: Recall abstract method syntax
In PHP, abstract methods are declared with the keyword abstract before the function signature and have no body (no curly braces).
Step 2: Check each option
abstract public function myMethod(); correctly declares an abstract method: abstract public function myMethod();. Options B and D have bodies or wrong order, and C uses incorrect syntax.
Final Answer:
abstract public function myMethod(); -> Option A
Quick Check:
Abstract method = abstract + no body [OK]
Hint: Abstract methods have no body and use 'abstract' keyword [OK]
Common Mistakes:
Adding curly braces {} to abstract methods
Wrong keyword order or missing 'abstract'
Trying to implement method body in abstract method
3. What will be the output of the following PHP code?
abstract class Animal {
abstract public function sound();
}
class Dog extends Animal {
public function sound() {
return "Bark";
}
}
$dog = new Dog();
echo $dog->sound();
medium
A. No output
B. Fatal error: Cannot instantiate abstract class Animal
C. Bark
D. Syntax error
Solution
Step 1: Understand class inheritance and method implementation
The abstract class Animal has an abstract method sound(). The class Dog extends Animal and implements sound() returning "Bark".
Step 2: Check object creation and method call
We create an object of Dog (which is allowed) and call sound(). This returns "Bark" which is echoed.
Final Answer:
Bark -> Option C
Quick Check:
Implemented abstract method returns 'Bark' [OK]
Hint: Only child classes can be instantiated, output from implemented method [OK]
Common Mistakes:
Trying to instantiate abstract class directly
Forgetting to implement abstract method in child
Expecting syntax error due to abstract method
4. Identify the error in the following PHP code:
abstract class Vehicle {
abstract public function startEngine();
}
class Car extends Vehicle {
// No implementation of startEngine()
}
$car = new Car();
$car->startEngine();
medium
A. Class Car must implement abstract method startEngine()
B. Cannot instantiate abstract class Vehicle
C. Method startEngine() should not be abstract
D. No error, code runs fine
Solution
Step 1: Check abstract method implementation
The abstract class Vehicle declares an abstract method startEngine(). The child class Car does not implement this method.
Step 2: Understand PHP rules for abstract methods
PHP requires all abstract methods to be implemented in child classes unless the child is also abstract. Since Car is concrete and missing the method, this causes an error.
Final Answer:
Class Car must implement abstract method startEngine() -> Option A
Hint: All abstract methods must be implemented in child classes [OK]
Common Mistakes:
Forgetting to implement abstract methods in child
Trying to instantiate abstract class directly
Assuming abstract methods can have bodies
5. Given the abstract class and two child classes below, what will be the output?
abstract class Shape {
abstract public function area();
}
class Square extends Shape {
private $side;
public function __construct($side) {
$this->side = $side;
}
public function area() {
return $this->side * $this->side;
}
}
class Circle extends Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function area() {
return 3.14 * $this->radius * $this->radius;
}
}
$shapes = [new Square(4), new Circle(3)];
foreach ($shapes as $shape) {
echo $shape->area() . " ";
}
hard
A. 62.82 61
B. 16 28.26
C. Error: Cannot instantiate abstract class Shape
D. 6 28.26
Solution
Step 1: Calculate area for Square
Square side is 4, so area = 4 * 4 = 16.
Step 2: Calculate area for Circle
Circle radius is 3, so area = 3.14 * 3 * 3 = 28.26.
Step 3: Understand output formatting
The code echoes each area followed by a space, so output is "16 28.26 " without newline.
Final Answer:
16 28.26 -> Option B
Quick Check:
Area calculations correct, output matches [OK]
Hint: Calculate each area, watch for spaces and no newline [OK]
Common Mistakes:
Confusing output formatting with newline
Trying to instantiate abstract class Shape
Incorrect area formulas
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.