Web programming

The concept of Inheritance in Object-Oriented Programming (OOP)

Ngoc Phuong

Ngoc Phuong

2021-03-03 . 150 view

Another important property in object-oriented programming (OOP) is inheritance. So, what is inheritance? Let's explore it in the article below.

I. Inheritance

In object-oriented programming, inheritance allows a class to inherit attributes and methods from other already defined classes.

Note: The inherited attributes and methods must have public or protected access.

The inherited class is also called the parent class, and the inheriting class is called the child class.

A class is defined as inheriting from another class by using the word extend.

For example, we have a class Strawberry inherited from the class Fruit, the Strawberry class will inherit the properties $name, $color, $price, $weight, and also methods __construct(), intro(), showPrice() but cannot inherit the method showWeight() because this method is private.

class Fruit {
  protected $name;
  protected $color;
  protected $price;
  protected $weight;
  public function __construct($name, $color, $price, $weight) {
    $this->name = $name;
    $this->color = $color;
    $this->price = $price;
    $this->weight = $weight;
  }
  public function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
  protected function showPrice() {
    echo "The price of {$this->name} is {$this->price}";
  }
  private function showWeight() {
    echo "The weight of {$this->name} is {$this->weight}";
  }
}

// The Strawberry class inherits from the Fruit class
class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
  }
  public function callShowPrice() {
    $this->showPrice(); // OK ==> Because the showPrice() method is protected
  }
}
$strawberry = new Strawberry("Strawberry", "red", "2000", "0.3kg");
$strawberry->message();   // OK because it is public
$strawberry->callShowPrice(); // OK because it is public
$strawberry->intro();   // OK because it is public
$strawberry->showPrice();   // ERROR because it is protected
$strawberry->showWeight();   // ERROR because it is private

II. Overriding inherited methods

The inherited methods from the parent class can be overridden by redefining the methods (using the same name) in the child class.

For example, we have a class Strawberry inherited from the parent class Fruit, in the child class Strawberry, the methods __construct, intro() override the methods __construct(), intro() in the parent class Fruit.

class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public $weight;
  public function __construct($name, $color, $weight) {
    parent::__construct($name, $color); // Call the parent class constructor
    $this->weight = $weight;
  }
  public function intro() {
    echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";
  }
}

$strawberry = new Strawberry("Strawberry", "red", 50);
$strawberry->intro();

III. Preventing inheritance, overriding

If you do not allow inheritance or overriding, you use final.

Example of not allowing the class Strawberry to inherit from the class Fruit.

final class Fruit {
  // some code
}

// will result in error
class Strawberry extends Fruit {
  // some code
}

Example of not allowing the class Strawberry to override the method intro() in the class Fruit.

class Fruit {
  final public function intro() {
    // some code
  }
}

class Strawberry extends Fruit {
  // will result in error
  public function intro() {
    // some code
  }
}

 

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