0
0
PHPprogramming~30 mins

Abstract classes and methods in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Abstract Classes and Methods in PHP
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use echo "\n"; to print a new line between outputs.