Lesson 5
Understanding Abstract Classes and Abstract Methods
Understanding Abstract Classes and Abstract Methods

Welcome back! Previously, you delved into polymorphism and learned how to create more flexible code structures using classes and inheritance. In this session, we will take a step further and explore a crucial aspect of object-oriented programming: abstract classes and abstract methods.

What You'll Learn

Abstract classes and abstract methods are essential when you want to define a common interface for a group of derived classes. They ensure that derived classes implement specific methods, enabling you to write more robust and scalable programs.

Let's revisit some of the key concepts through the following PHP code example:

php
1<?php 2 3// Define an abstract class Shape. 4abstract class Shape { 5 // Abstract method for calculating the area 6 abstract public function area(): float; 7 8 // Abstract method for calculating the perimeter 9 abstract public function perimeter(): float; 10} 11 12// Define a Circle class that inherits from Shape 13class Circle extends Shape { 14 private $radius; 15 16 // Constructor to initialize the radius 17 public function __construct(float $radius) { 18 $this->radius = $radius; 19 } 20 21 // Implement the area and perimeter methods 22 public function area(): float { 23 return 3.14 * $this->radius * $this->radius; 24 } 25 26 public function perimeter(): float { 27 return 2 * 3.14 * $this->radius; 28 } 29} 30 31// Define a Rectangle class that inherits from Shape 32class Rectangle extends Shape { 33 private $width; 34 private $height; 35 36 // Constructor to initialize the width and height 37 public function __construct(float $width, float $height) { 38 $this->width = $width; 39 $this->height = $height; 40 } 41 42 // Implement the area and perimeter methods 43 public function area(): float { 44 return $this->width * $this->height; 45 } 46 47 public function perimeter(): float { 48 return 2 * ($this->width + $this->height); 49 } 50} 51 52$circle = new Circle(5); 53$rectangle = new Rectangle(4, 6); 54 55echo "Circle Area: " . $circle->area() . ", Perimeter: " . $circle->perimeter() . "\n"; 56echo "Rectangle Area: " . $rectangle->area() . ", Perimeter: " . $rectangle->perimeter() . "\n"; 57 58// Using Shape type to reference a Circle object 59$shape = new Circle(3); 60echo "Shape Area: " . $shape->area() . ", Perimeter: " . $shape->perimeter() . "\n"; 61 62?>

In this example, we define an abstract class Shape with two abstract methods: area and perimeter. Derived classes such as Circle and Rectangle implement these methods.

Let's now understand the abstract class and abstract methods in more detail:

An abstract class is a class that contains at least one abstract method — a method declared with the abstract keyword and without implementation. An abstract class cannot be instantiated, but it can be used as a base class for other classes. In the example above, Shape is an abstract class.

The derived classes Circle and Rectangle inherit from the abstract class Shape. They must implement the abstract methods area and perimeter to provide concrete implementations. If a derived class does not implement all the abstract methods, it will also be considered an abstract class and cannot be instantiated.

Why It Matters

Abstract classes and abstract methods offer a way to enforce certain patterns and rules in your code. They allow you to design a system where different types of objects can be treated uniformly while ensuring that specific behaviors are implemented in each derived class.

By mastering abstract classes and abstract methods, you'll be able to:

  1. Create more organized and readable code: You'll have a clear structure that dictates how certain functions should behave.
  2. Encourage code reusability: Common code can reside in abstract base classes, reducing redundancy.
  3. Enhance flexibility: Easily add new types of derived classes without modifying existing code — a key principle of software design.

Intrigued? Let's move on to the practice section and solidify these concepts together. You're on your way to becoming proficient in building sophisticated and maintainable systems!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.