Inheritance in OOP occurs when a class derives from another class.
The child class inherits all the public and protected properties and methods from the parent class, and it can also define its own properties and methods.
An inherited class is defined using the extends
keyword.
Here’s an example:
<?php 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}.”; } } // Strawberry is inherited from Fruit class Strawberry extends Fruit { public function message() { echo “Am I a fruit or a berry? “; } } $strawberry = new Strawberry(“Strawberry”, “red”); $strawberry->message(); $strawberry->intro(); ?> |
The Strawberry
class inherits from the Fruit
class.
This means that the Strawberry
class can access the public $name
and $color
properties, as well as the __construct()
and intro()
methods from the Fruit
class due to inheritance.
Additionally, the Strawberry
class defines its own method: message()
.
In the previous chapter, we learned that protected properties or methods can be accessed within the class itself and by any classes that inherit from it. What does this mean?
Let’s explore with an example:
<?php class Fruit { public $name; public $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } protected function intro() { echo “The fruit is {$this->name} and the color is {$this->color}.”; } } class Strawberry extends Fruit { public function message() { echo “Am I a fruit or a berry? “; } } // Try to call all three methods from outside class $strawberry = new Strawberry(“Strawberry”, “red”); // OK. __construct() is public $strawberry->message(); // OK. message() is public $strawberry->intro(); // ERROR. intro() is protected ?> |
In the example above we see that if we try to call a protected method (intro()) from outside the class, we will receive an error. public methods will work fine!
Let’s look at another example:
<?php class Fruit { public $name; public $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } protected function intro() { echo “The fruit is {$this->name} and the color is {$this->color}.”; } } class Strawberry extends Fruit { public function message() { echo “Am I a fruit or a berry? “; // Call protected method from within derived class – OK $this -> intro(); } } $strawberry = new Strawberry(“Strawberry”, “red”); // OK. __construct() is public $strawberry->message(); // OK. message() is public and it calls intro() (which is protected) from within the derived class ?> |
In the example above, everything works correctly because we are calling the protected method (intro()
) from within the derived class.
Inherited methods can be overridden by redefining them (using the same name) in the child class.
In the example below, the __construct()
and intro()
methods in the Strawberry
class will override the __construct()
and intro()
methods from the Fruit
class.
<?php 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) { $this->name = $name; $this->color = $color; $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(); ?> |
The final
keyword can be used to prevent a class from being inherited or to stop methods from being overridden.
The following example demonstrates how to prevent class inheritance:
<?php final class Fruit { // some code } // will result in error class Strawberry extends Fruit { // some code } ?> |
The following example illustrates how to prevent method overriding:
<?php class Fruit { final public function intro() { // some code } } class Strawberry extends Fruit { // will result in error public function intro() { // some code } } ?> |