The concept of Inheritance in Object-Oriented Programming (OOP)
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
}
}
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 . 728 view
-
Call of Duty: Black Ops 6 - Intense, Mysterious, and Surprising Warfare
09-02-2024 . 658 view
-
The "End of Life" for Windows 10: A Massive E-Waste Threat and Sustainable Solutions
08-18-2024 . 636 view
-
Casio WS-B1000: The Perfect Blend of Traditional Watch and Smartwatch
08-11-2024 . 563 view
-
Call of Duty: Black Ops 6 Consumes 300GB SSD: Xbox Series X/S Gamers Take Note
09-02-2024 . 524 view
0 feedback