HomePHP OOPSWhat is Polymorphism

What is Polymorphism

Polymorphism is one of the four fundamental principles of Object-Oriented Programming (OOP) in PHP. It allows objects of different classes to be treated as objects of a common super class. This is achieved through method overriding and interfaces.

🔹 What is Polymorphism?

Polymorphism means “many forms.” In PHP OOP, it allows different classes to have methods with the same name but different implementations.

There are two types of Polymorphism:

  1. Method Overriding (Run-time Polymorphism)
  2. Method Overloading (PHP does not support function overloading directly like Java or C#)

🔹 1. Polymorphism Using Method Overriding

This is when a child class redefines a method inherited from a parent class.

Example:

<?php
class Animal {
    public function makeSound() {
        return "Some generic animal sound";
    }
}

class Dog extends Animal {
    public function makeSound() {
        return "Bark";
    }
}

class Cat extends Animal {
    public function makeSound() {
        return "Meow";
    }
}

$animals = [new Dog(), new Cat(), new Animal()];

foreach ($animals as $animal) {
    echo $animal->makeSound() . PHP_EOL;
}
?>

Output:

Bark
Meow
Some generic animal sound

Here, makeSound() is overridden in the Dog and Cat classes.


🔹 2. Polymorphism Using Interfaces

Interfaces allow multiple classes to implement the same method signature in different ways.

Example:

<?php
interface Shape {
    public function area();
}

class Circle implements Shape {
    private $radius;

    public function __construct($radius) {
        $this->radius = $radius;
    }

    public function area() {
        return pi() * $this->radius * $this->radius;
    }
}

class Rectangle implements Shape {
    private $width, $height;

    public function __construct($width, $height) {
        $this->width = $width;
        $this->height = $height;
    }

    public function area() {
        return $this->width * $this->height;
    }
}

$shapes = [new Circle(5), new Rectangle(4, 6)];

foreach ($shapes as $shape) {
    echo "Area: " . $shape->area() . PHP_EOL;
}
?>

Output:

Area: 78.539816339745
Area: 24

Here, both Circle and Rectangle implement the Shape interface but define area() differently.


🔹 Polymorphism with Abstract Classes

Abstract classes allow method declarations that must be implemented in child classes.

Example:

<?php
abstract class Vehicle {
    abstract public function fuelType();
}

class Car extends Vehicle {
    public function fuelType() {
        return "Petrol or Diesel";
    }
}

class ElectricCar extends Vehicle {
    public function fuelType() {
        return "Electricity";
    }
}

$vehicles = [new Car(), new ElectricCar()];

foreach ($vehicles as $vehicle) {
    echo $vehicle->fuelType() . PHP_EOL;
}
?>

Output:

Petrol or Diesel
Electricity

Here, Vehicle is an abstract class, and Car and ElectricCar override fuelType().


🔹 Key Takeaways

✅ Polymorphism allows objects of different types to be treated as the same type (e.g., all shapes implement Shape).
✅ Method Overriding enables redefining a method from the parent class in a child class.
✅ Interfaces allow different classes to implement the same method differently.
✅ Abstract classes help enforce method implementation in derived classes.

Would you like me to explain any part in more detail? 🚀

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *