PHP - Inheritance and Polymorphism
Given the abstract class and two child classes below, what will be the output?
abstract class Shape {
abstract public function area();
}
class Square extends Shape {
private $side;
public function __construct($side) {
$this->side = $side;
}
public function area() {
return $this->side * $this->side;
}
}
class Circle extends Shape {
private $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function area() {
return 3.14 * $this->radius * $this->radius;
}
}
$shapes = [new Square(4), new Circle(3)];
foreach ($shapes as $shape) {
echo $shape->area() . " ";
}