DEV Community

abdalshafiealmajdoup
abdalshafiealmajdoup

Posted on

OOP ( Abstract Class VS Polymorphism Class ) .

Object-oriented programming (OOP) is a programming paradigm that is widely used to develop software applications. It emphasizes the use of objects and classes to organize and structure code. Two important concepts in OOP are abstract and polymorphism. Although these concepts are related, they are different and serve distinct purposes. In this article, we will explore the differences between abstract and polymorphism, and provide real-world examples using

the PHP programming language.

abstract class Animal {
   protected $name;

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

   abstract public function speak();
}

Enter fullscreen mode Exit fullscreen mode

This abstract class defines a constructor that initializes the name property, and an abstract method called "speak". This class cannot be instantiated on its own, but it can be extended by other classes that implement the "speak" method. For example, we can create a subclass called "Dog" that extends the Animal class and implements the "speak" method:

class Dog extends Animal {
   public function speak() {
      return "Woof!";
   }
}

Enter fullscreen mode Exit fullscreen mode

Polymorphism

Polymorphism is another important concept in OOP that allows you to use a single interface to represent multiple types of objects. Polymorphism is based on the principle of "one interface, multiple implementations." In other words, you can use the same method or function to perform different actions based on the context in which it is used. Polymorphism is often used in conjunction with inheritance and interfaces to achieve flexibility and extensibility in OOP.

In PHP, you can achieve polymorphism using inheritance and interfaces. When a class implements an interface, it must implement all the methods defined in that interface. This allows you to use objects of different classes that implement the same interface interchangeably. Here's an example of polymorphism in PHP:

interface Shape {
   public function getArea();
}

class Square implements Shape {
   private $side;

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

   public function getArea() {
      return pow($this->side, 2);
   }
}

class Circle implements Shape {
   private $radius;

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

   public function getArea() {
      return pi() * pow($this->radius, 2);
   }
}

Enter fullscreen mode Exit fullscreen mode

This code defines an interface called "Shape" with a single method called "getArea". It also defines two classes, "Square" and "Circle", that implement the "Shape" interface and provide their own implementation of the "getArea" method. We can now create objects of both classes and use them interchangeably:

$square = new Square(5);
$circle = new Circle(3);

echo $square->getArea(); // output: 25
echo $circle->getArea(); // output: 28.274333882308

Enter fullscreen mode Exit fullscreen mode

created objects of the "Square" and "Circle" classes and called the "getArea" method on both objects. Despite having different implementations, both objects were able to provide their own area calculations through the common interface defined by the "Shape" interface. This is an example of polymorphism in action.

Difference between Abstract and Polymorphism

Abstract and polymorphism are related concepts, but they serve different purposes. Abstract is a way to define a template or a blueprint for a class without actually creating an instance of it. Abstract classes cannot be instantiated on their own, but they can be used as a base class for other classes that extend them. Abstract classes provide a common interface or behavior for a group of related classes.

Polymorphism, on the other hand, allows you to use a single interface to represent multiple types of objects. Polymorphism is based on the principle of "one interface, multiple implementations." It allows you to use objects of different classes that implement the same interface interchangeably. Polymorphism is often used in conjunction with inheritance and interfaces to achieve flexibility and extensibility in OOP.

Real-World Examples in PHP

To further illustrate the differences between abstract and polymorphism, let's look at some real-world examples in PHP.

Example 1: Abstract

Suppose we are developing a game that involves different types of characters, such as warriors, wizards, and archers. Each character has its own set of abilities and attributes, but they all share some common properties, such as health and mana. We can use an abstract class to define a template for the characters, like this:

abstract class Character {
   protected $name;
   protected $health;
   protected $mana;

   public function __construct($name, $health, $mana) {
      $this->name = $name;
      $this->health = $health;
      $this->mana = $mana;
   }

   abstract public function attack();
}


Enter fullscreen mode Exit fullscreen mode

This abstract class defines the properties of the characters and an abstract method called "attack". Each subclass that extends this class must implement its own version of the "attack" method.

class Warrior extends Character {
   public function attack() {
      // Warrior-specific attack logic
   }
}

class Wizard extends Character {
   public function attack() {
      // Wizard-specific attack logic
   }
}

class Archer extends Character {
   public function attack() {
      // Archer-specific attack logic
   }
}

Enter fullscreen mode Exit fullscreen mode

Each subclass defines its own implementation of the "attack" method, which allows them to have their own unique abilities and attacks. By using an abstract class, we can define a common interface for all the characters and ensure that they all have the same properties and behaviors.

Example 2: Polymorphism

Suppose we are developing a web application that allows users to upload different types of files, such as images, videos, and documents. Each file type has its own set of properties and methods, but they all share some common properties, such as a name and a size. We can use an interface to define a common interface for all the file types, like this:

interface File {
   public function getName();
   public function getSize();
   public function display();
}

Enter fullscreen mode Exit fullscreen mode

This interface defines three methods that all the file types must implement: "getName", "getSize", and "display". Each file type implements these methods in its own way, based on its own unique properties and methods.

class Image implements File {
   private $name;
   private $size;
   private $resolution;

   public function __construct($name, $size, $resolution) {
      $this->name = $name;
      $this->size = $size;
      $this->resolution = $resolution;


Enter fullscreen mode Exit fullscreen mode

Top comments (0)