Inheritance: Abstract classes and interfaces

What is inheritance? It's a programming principle used in the PHP object model which allows a class (child) to inherit another class' (parent) protected or public methods, properties and constants, retaining the parent's original functionality, unless these are overriden by the child class.

What is an abstract class? A class that contains at least one abstract method, which means only the signature of the method is defined. For example:



abstract Class Parent {

abstract protected function defineMyFunction().

}

Class Child extends Parent {

    protected function defineMyFunction() {
        return "I am an abstract function in my Parent class";
    }
}

As childish as this example might seem , it quite easily represents the aforementioned definition.

When a  class (or child class) extends from an abstract class (or parent class), it must define all methods declared as abstract. In fact, if you are using an IDE, it will probably mark the whole class with a bug if you forget to do so. 

What is an interface? It is frequently described as an agreement  or a promise to implement the same public methods described in the interface. Interfaces only contain method signatures, which means, they specify which methods the classes must implement, but not how they are going to be implemented. They do not contain variables, but they can contain constants. These constants work in a similar way to class constants, but they cannot be overriden by the inherited class. But let's have a look at an example:



interface Animal {

    public function makeSound().
    public function eat().
    public function beFriendsWith().    
    public function hateAnimals().
}

Class Dog impements Animal {

    public function makeSound() 
    {
        return "I bark".
    }
    public function eat() 
    {
        return "I eat meatballs".
    }
    public function beFriendsWith() 
    {
        return "I am friends with other dogs".
    }
    public function hateAnimals() 
    {
        return "I hate cats".
    }
}

What is the difference between them? Simply put, an abstract class is like a raw template of another class and an interface is the promise to include it's methods in the class that implements it. It is also important to remember that interfaces only contain method signatures, not their implementation, and that classes are extended while interfaces are implemented. 

How can this be useful? It is quite tricky, at leas for me, to find good examples when explaining concepts, but doing so, means I have fully understood the theory and that I will be able to use it in real world when developing. So imagine you are developing a software for a shopping app. All of the products you add to your shopping card are going to have things in common, like a name, a price before taxes, a price after taxes. And you will do the same thing with those products, right? like adding the product to your cart, or applying a discount when buying more than one unit. Identifying and applying those patterns to your code when developing is probably one of the trickiest skills to acquire when learning how to code. That's why I highly recommend anyone trying, to use paper and a pen first to kind of "draw" templates or patterns. I wish I had known when I started my very first app. Good news is, when you "make to learn", that is, when you are still learning as you develop something, you are able to identify the "disasters" you made when you started your app, and that only means one thing: You have learned!

Happy coding everyone.

Comments

Popular posts from this blog

SOLID: The Single-Responsibility Principle (SRP)

Symfony HTTP client