⇧
vadnica-logo
My Services About the Project Telegram O meni O meni

OOP Basics: Inheritance

Inheritance is one of the most important pillars of object-oriented programming. It allows us to create a new class that automatically inherits all public and protected properties and methods of an existing class. In PHP, we achieve this magic using the extends keyword.

With this concept, we create a natural hierarchy between classes, which in programming is described as a Parent → Child or Superclass → Subclass relationship. Imagine the following real-world example:

EXAMPLE
RESULT
The result will be displayed here...

Why is Inheritance Such a Powerful Tool?

The main goal of inheritance is to drastically reduce code duplication in your projects (strictly following the DRY principle). Instead of writing identical functions for starting the engine or driving for a Car, a Motorcycle, and a Truck, you write this logic only once in the parent Vehicle class. All other subclasses then simply use it or adapt (override) it as needed.

By using inheritance, your applications become extremely modular, easier to maintain, and ready for quick additions of new, more advanced functionalities without interfering with already working code.