Web programming

Polymorphism in Object-Oriented Programming (OOP)

Ngoc Phuong

Ngoc Phuong

2021-03-07 . 113 view

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.

Ngoc Phuong
Ngoc Phuong

Web Developer

Thank you for visiting my website. My name is Ngoc Phuong, and I have over 10 years of experience in website development. I am confident in stating that I am an expert in creating impressive and effective websites. If you need a website designed, please feel free to contact me via email at [email protected].

0 feedback