Understanding Polymorphism in Object-Oriented Programming
How polymorphism makes the code better
In computer science, polymorphism describes the idea you can entry objects of various sorts by the identical interface. In less complicated phrases, it may be outlined as having a distant management with a single button that does various things relying on what you’re pointing it at.
In this article, we are going to examine a code with out the idea of polymorphism after which talk about how making use of the idea of polymorphism could make the code higher. Nicely first take a look at the next code with out polymorphism and attempt to perceive what are the issues with this code:
<?php
class Product {
protected $identify;
protected $worth;
public operate __construct($identify, $worth)
$this->identify = $identify;
$this->worth = $worth;
public operate displayDetails($kind, $additional) {
if ($kind == "clothing") {
echo "Clothing: {$this->name}, Size: {$extra}, Price: {$this->price}\n";
} elseif ($kind == "electronics") {
echo "Electronics: {$this->name}, Brand: {$extra}, Price: {$this->price}\n";
}
}
}
// Create new product objects
$product_cloth = new Product("T-Shirt", 19.99);
$product_electronics = new Product("iPhone15", 199.99);
// Show particulars for clothes with measurement XL
$product->displayDetails("clothing", "XL");
// Show particulars for electronics with model iPhone
$product->displayDetails("electronics", "iPhone");
?>
Now, there are a number of issues with this code. To begin with, if we wish to add one other product kind, let’s say guide
we might want to add yet another if situation within the product class. Nonetheless, a category ought to be capable to be prolonged flexibly with out modifying the category itself.
One other drawback with this code is, for instance, identify and worth are widespread params for each electronics kind of merchandise and clothes kind of merchandise. However for instance, there may be some unusual attribute for various kinds of merchandise, which on this case was applied by including one other param named additional
. But when there are 10 such attributes, you possibly can simply guess how messy the code shall be.
So, to do away with such issues, we will rewrite the code utilizing the idea of polymorphism like this:
<?php
class Product {
protected $identify;
protected $worth;
public operate __construct($identify, $worth)
$this->identify = $identify;
$this->worth = $worth;
public operate displayDetails() {
echo "Product: {$this->name}, Price: {$this->price}\n";
}
}
class Guide extends Product {
non-public $creator;
public operate __construct($identify, $worth, $creator)
guardian::__construct($identify, $worth);
$this->creator = $creator;
public operate displayDetails() {
echo "Book: {$this->name}, Author: {$this->author}, Price: {$this->price}\n";
}
}
class Electronics extends Product {
non-public $model;
public operate __construct($identify, $worth, $model)
guardian::__construct($identify, $worth);
$this->model = $model;
public operate displayDetails() {
echo "Electronics: {$this->name}, Brand: {$this->brand}, Price: {$this->price}\n";
}
}
class Clothes extends Product {
non-public $measurement;
public operate __construct($identify, $worth, $measurement)
public operate displayDetails() {
echo "Clothing: {$this->name}, Size: {$this->size}, Price: {$this->price}\n";
}
}
// Operate to show product particulars
operate displayProductDetails(Product $product) {
$product->displayDetails();
}
// Creating objects
$guide = new Guide("The Great Gatsby", 15.99, "F. Scott Fitzgerald");
$electronics = new Electronics("Smartphone", 499.99, "Samsung");
$clothes = new Clothes("T-Shirt", 19.99, "M");
// Displaying particulars utilizing polymorphism
displayProductDetails($guide);
displayProductDetails($electronics);
displayProductDetails($clothes);
?>
The benefit of this code is, to start with, you possibly can maintain including extra varieties of merchandise simply by extending the Product class.
Then, for various kinds of merchandise, you possibly can add any attributes with out affecting the guardian class. Simply overriding the displayDetails() operate and implementing it as wanted will do it.
Additionally if you happen to take a look at the `displayProductDetails` operate, you possibly can see that we will deal with all of the various kinds of merchandise the identical approach. we will name the identical operate nevertheless it provides various kinds of outcomes for various kinds of merchandise.
Static polymorphism, often known as compile-time polymorphism, happens when the strategy to be invoked is decided at compile time. In PHP, technique overloading is a type of static polymorphism. Methodology overloading permits a category to have a number of strategies with the identical identify however with completely different parameters or argument lists.
Right here’s an instance of technique overloading in PHP:
<?php
class MathOperations {
// Methodology so as to add two numbers
public operate add($num1, $num2) {
return $num1 + $num2;
}
// Methodology overloading so as to add three numbers
public operate add($num1, $num2, $num3) {
return $num1 + $num2 + $num3;
}
}
$math = new MathOperations();
echo $math->add(2, 3) . "\n"; // Output: 5
echo $math->add(2, 3, 4) . "\n"; // Output: 9
?>
Within the above instance, the MathOperations
class has two add()
strategies. The primary one takes two parameters, and the second takes three parameters. The PHP interpreter decides which technique to name primarily based on the variety of arguments supplied throughout the operate name. That is decided at compile time.
Dynamic polymorphism, often known as runtime polymorphism, happens when the strategy to be invoked is decided at runtime. In PHP, technique overriding in subclass is a type of dynamic polymorphism. Methodology overriding permits a subclass to offer a particular implementation of a technique that’s already outlined in its superclass. The primary polymorphism instance which was given on this article is an instance of dynamic polymorphism.
So, that’s it. From this dialogue we noticed how the idea of polymorphism elevated the code’s high quality by giving higher abstraction, lowering code duplication and enhancing flexibility and extensibility of the the category. This makes it simpler so as to add new varieties of objects or modify current ones with out having to alter plenty of code. Completely happy coding!