Inheritance
Reuse code and extend functionality with inheritance.
Inheritance is a free PHP Academy lesson on CoddyKit — lesson 3 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Inheritance in PHP
Welcome to the next lesson! In this lesson, you’ll learn about inheritance, a core concept in Object-Oriented Programming (OOP) that allows you to extend the functionality of a class. Let’s get started!

2
What Is Inheritance?
Inheritance is a mechanism in OOP that allows one class (child class) to inherit properties and methods from another class (parent class). This promotes code reuse and hierarchy in programming.
Example: A Car class can serve as a parent for Sedan and SUV classes.
3
Defining Inheritance
Use the extends keyword to create a child class that inherits from a parent class. Example:
Tip: Use inheritance to avoid duplicating code.
<h2>Defining Inheritance</h2>
<p>Use the <code>extends</code> keyword to create a child class that inherits from a parent class. Example:</p>
4
Overriding Methods
Child classes can override methods from the parent class to provide specific functionality. Example:
<?php
class Vehicle {
public function drive() {
echo "Driving a generic vehicle.";
}
}
class Car extends Vehicle {
public function drive() {
echo "Driving a car.";
}
}
$car = new Car();
$car->drive(); // Outputs: Driving a car.
?>
5
The parent Keyword
Use the parent keyword to call a parent class method inside an overridden method. Example:
<?php
class Vehicle {
public function start() {
echo "Starting the vehicle.";
}
}
class Car extends Vehicle {
public function start() {
parent::start();
echo " Starting the car engine.";
}
}
$car = new Car();
$car->start(); // Outputs: Starting the vehicle. Starting the car engine.
?>
6
Benefits of Inheritance
Inheritance offers the following advantages:
- Promotes code reuse.
- Establishes a clear class hierarchy.
- Enables polymorphism by allowing child classes to override methods.
7
Protected Members
Inheritance also works with protected members. Protected properties and methods are accessible to child classes but not outside the class hierarchy. Example:
Key Point: Use protected to share members between parent and child classes securely.
<?php
class Vehicle {
protected $fuel;
public function setFuel($fuel) {
$this->fuel = $fuel;
}
}
class Car extends Vehicle {
public function getFuel() {
return $this->fuel;
}
}
$car = new Car();
$car->setFuel("Gasoline");
echo "Fuel: " . $car->getFuel(); // Outputs: Fuel: Gasoline
?>
8
Abstract Classes
An abstract class provides a base structure for child classes but cannot be instantiated itself. Example:
Key Point: Use abstract classes when you want to enforce certain methods in child classes.
<?php
abstract class Vehicle {
abstract public function start();
}
class Car extends Vehicle {
public function start() {
echo "Starting the car engine.";
}
}
$car = new Car();
$car->start(); // Outputs: Starting the car engine.
?>
9
Final Keyword
The final keyword prevents further inheritance or overriding. Example:
Tip: Use final to protect methods or classes from being extended.
<?php
class Vehicle {
final public function start() {
echo "Starting the vehicle.";
}
}
class Car extends Vehicle {
// Cannot override the start method
}
?>
10
11
Great Job!
Congratulations! You’ve learned about inheritance in PHP, including how to extend classes, override methods, and use abstract classes. In the next lesson, we’ll explore encapsulation and access modifiers. Let’s keep coding!

Frequently asked questions
Is the “Inheritance” lesson free?
Yes — the full text of “Inheritance” is free to read here on the web, and the PHP Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Inheritance”?
Reuse code and extend functionality with inheritance. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Inheritance” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.