Polymorphism in Object-Oriented Programming (OOP)
I. Understanding Polymorphism in Object-Oriented Programming (OOP)
1. What is Polymorphism?
Polymorphism refers to the ability of different objects to respond in their own way to the same action.
When multiple child classes inherit from a parent class and exhibit different behaviors, it is called polymorphism, or when multiple classes implement the same interface, it is also referred to as polymorphism.
For example, we have an Animal class that defines an eat() method. The classes Rabbit and Lion inherit from the Animal class. However, each animal eats differently - Rabbit eats carrots, while Lion eats meat. Therefore, we use the override technique to redefine the eat() method for Rabbit and Lion classes.
class Animal{
public $name;
public function setName($name){
$this->name = $name;
}
public function eat(){
echo "The animal {$this->name} is eating ...";
}
}
class Lion extends Animal{
public function eat(){
echo "The {$this->name} is eating rabbit meat";
}
}
class Rabbit extends Animal{
public function eat(){
echo "The {$this->name} is eating carrots";
}
}
$lion = new Lion();
$lion->setName("Lion");
$lion->eat();
echo "
";
$rabbit = new Rabbit();
$rabbit->setName("Rabbit");
$rabbit->eat(); // Removed the unnecessary parameter "Rabbit"
From the example above, it is clear that polymorphism always exists alongside inheritance.
2. The Essence of Polymorphism
The essence of polymorphism is a technique that allows altering the content of the same behavior (function) in both parent and child classes, or in other words, redefining a function from the parent class in the child class.
For example, let's consider the problem of calculating the area of a square and a rectangle as follows
abstract class HinhHoc{
public $name;
public function __construct($name) {
$this->name = $name;
}
public function tieuDe(){
echo "Dien tich hinh {$this->name} la: ";
}
abstract public function tinhDienTich();
}
class HinhVuong extends HinhHoc{
public $canh = 1;
public function ganDoDai($canh){
$this->canh = $canh;
}
public function tinhDienTich(){
$dienTich = $this->canh*$this->canh;
return $dienTich;
}
}
class HinhChuNhat extends HinhHoc{
public $rong = 1;
public $dai = 1;
public function ganDoDai($rong,$dai){
$this->rong = $rong;
$this->dai = $dai;
}
public function tinhDienTich(){
$dienTich = $this->rong*$this->dai;
return $dienTich;
}
}
$hinhVuong = new HinhVuong("Hinh Vuong");
$hinhVuong->tieuDe();
$hinhVuong->ganDoDai(4);
echo $hinhVuong->tinhDienTich();
echo "
";
$hinhChuNhat = new HinhChuNhat("Hinh Chu Nhat");
$hinhChuNhat->tieuDe();
$hinhChuNhat->ganDoDai(4,5);
echo $hinhChuNhat->tinhDienTich();
echo "
";
The above example utilized the override technique to rewrite the calculateArea(), setDimensions() methods for each object, Square and Rectangle.
Submit feedback
Your email address will not be made public. Fields marked are required *
Search
Trend
-
What is Black Myth: Wukong? Detailed Guide on System Requirements and Gameplay
08-21-2024 . 821 view
-
Call of Duty: Black Ops 6 - Intense, Mysterious, and Surprising Warfare
09-02-2024 . 708 view
-
The "End of Life" for Windows 10: A Massive E-Waste Threat and Sustainable Solutions
08-18-2024 . 685 view
-
Casio WS-B1000: The Perfect Blend of Traditional Watch and Smartwatch
08-11-2024 . 603 view
-
Lost All Money by Trusting in "Elon Musk Deepfake": A Cautionary Tale of Deepfake
08-21-2024 . 572 view
0 feedback