Menu
Coddy logo textTech

Abstract Classes

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 25 of 91.

An abstract class is a class that cannot be instantiated directly. It serves as a blueprint for other classes, defining methods that child classes must implement. You create an abstract class using the abstract keyword.

<?php
abstract class Shape {
    protected $color;
    
    public function __construct($color) {
        $this->color = $color;
    }
    
    abstract public function calculateArea();
    
    public function getColor() {
        return $this->color;
    }
}

class Rectangle extends Shape {
    private $width;
    private $height;
    
    public function __construct($color, $width, $height) {
        parent::__construct($color);
        $this->width = $width;
        $this->height = $height;
    }
    
    public function calculateArea() {
        return $this->width * $this->height;
    }
}

$rect = new Rectangle("blue", 5, 3);
echo $rect->calculateArea();

Output:

15

The Shape class declares an abstract method calculateArea() without providing an implementation. Any class extending Shape must implement this method. Notice that abstract classes can also contain regular methods like getColor() that child classes inherit directly.

Abstract methods define what a child class must do, but not how. This ensures all shapes have a calculateArea() method while letting each shape calculate its area differently.

Key Point: Abstract classes combine the structure of inheritance with enforced requirements. Use them when you want to share code among related classes while ensuring certain methods are implemented by each child class.

challenge icon

Challenge

Easy

Let's build a media player system that demonstrates how abstract classes enforce a contract while allowing different media types to implement their own playback behavior.

You'll organize your code across three files:

  • Media.php — Define an abstract Media class that serves as the blueprint for all media types. It should have a protected $title property and a constructor that accepts and sets the title. Include an abstract method called play() that child classes must implement. Also add a regular method getTitle() that returns the title — this concrete method will be inherited by all child classes.
  • Song.php — Define a Song class that extends Media. Include the Media file at the top. This class adds an $artist property. The constructor should accept title and artist — use parent::__construct() for the title, then set the artist yourself. Implement the required play() method to return "Playing song: [title] by [artist]".
  • Podcast.php — Define a Podcast class that extends Media. Include the Media file at the top. This class adds an $episode property (an integer). The constructor should accept title and episode number — use parent::__construct() for the title, then set the episode. Implement the required play() method to return "Playing podcast: [title] - Episode [episode]".

In main.php, include both the Song and Podcast files. You'll receive four inputs: the song title, artist name, podcast title, and episode number (as a string to convert to an integer). Create a Song and a Podcast object with these values. Print the result of calling play() on the song first, then on the podcast, each on its own line.

This challenge shows how abstract classes define a contract — every media type must have a play() method, but each type implements it differently. The abstract Media class cannot be instantiated directly; it only exists to provide structure and shared functionality to its children.

Cheat sheet

An abstract class is a class that cannot be instantiated directly. It serves as a blueprint for other classes, defining methods that child classes must implement. Use the abstract keyword to create an abstract class.

abstract class Shape {
    protected $color;
    
    public function __construct($color) {
        $this->color = $color;
    }
    
    abstract public function calculateArea();
    
    public function getColor() {
        return $this->color;
    }
}

Child classes must implement all abstract methods:

class Rectangle extends Shape {
    private $width;
    private $height;
    
    public function __construct($color, $width, $height) {
        parent::__construct($color);
        $this->width = $width;
        $this->height = $height;
    }
    
    public function calculateArea() {
        return $this->width * $this->height;
    }
}

Key characteristics:

  • Abstract classes cannot be instantiated directly
  • Abstract methods have no implementation in the abstract class
  • Child classes must implement all abstract methods
  • Abstract classes can contain both abstract and regular methods
  • Regular methods in abstract classes are inherited by child classes

Try it yourself

<?php
require_once 'Song.php';
require_once 'Podcast.php';

// Read inputs
$songTitle = trim(fgets(STDIN));
$artistName = trim(fgets(STDIN));
$podcastTitle = trim(fgets(STDIN));
$episodeNumber = intval(trim(fgets(STDIN)));

// TODO: Create a Song object with songTitle and artistName

// TODO: Create a Podcast object with podcastTitle and episodeNumber

// TODO: Print the result of calling play() on the song

// TODO: Print the result of calling play() on the podcast

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming