Our digital world is technically advanced. It has invented Smartphones, but Smartphones are classified into distinct brands. In this tab, we are going to create subclasses of Smartphones: classes Realme, Apple, and Samsung. Creating subclasses is useful when you want to create a more specialized version of a current class that you have. If it is just a specialized class it means that the current functionality will mostly remain the same except for some minor adjustments. By using class inheritance, we don't have to repeat ourselves. This is our current class of Smartphone (with an additional default app):
Say we wanted to create a new class and we wanted its functions and properties to be used by other classes, but there were some additional functions and properties that we wanted to add to it. In this case, we would have our new class inherit or extend the original class (parent class).
Inheritance
Our digital world is technically advanced. It has invented Smartphones, but Smartphones are classified into distinct brands. In this tab, we are going to create subclasses of Smartphones: classes Realme, Apple, and Samsung. Creating subclasses is useful when you want to create a more specialized version of a current class that you have. If it is just a specialized class it means that the current functionality will mostly remain the same except for some minor adjustments. By using class inheritance, we don't have to repeat ourselves. This is our current class of Smartphone (with an additional default app):
class Smartphone { public $apps = array("Playstore"); public $model; public $sim1; public function __construct() { echo "Welcome!"; $this->model = "Realme 8 5g"; } public function install($app_name) { array_push($this->apps, $app_name); echo "Successfully installed $app_name."; $this->list_apps(); } public function list_apps() { echo "<br>All apps:"; foreach($this->apps as $app) { echo "$app "; } } }
Say we wanted to create a new class and we wanted its functions and properties to be used by other classes, but there were some additional functions and properties that we wanted to add to it. In this case, we would have our new class inherit or extend the original class (parent class).
This is what our inheritance would look like:
...