Skip to content
Justice Tech School

Inheritance and Interfaces

extends inherits from a parent class and parent:: calls the parent version. An interface lists methods a class must implement. abstract classes cannot be instantiated. A class can implement many interfaces.

Example
interface Speaks { public function speak(): string; }
abstract class Animal implements Speaks {
  public function __construct(protected string $name) {}
}
class Dog extends Animal {
  public function speak(): string { return "{$this->name} says woof"; }
}
class Cat extends Animal {
  public function speak(): string { return "{$this->name} says meow"; }
}
foreach ([new Dog("Rex"), new Cat("Tom")] as $pet) {
  echo $pet->speak(), "\n";
}

Result

Rex says woof
Tom says meow

Exercise

Inherit from Animal.

class Dog  Animal {}

Quiz Test

What does an interface define?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.