DEV Community

Cover image for OOP CAN BE EASY
Salma
Salma

Posted on

OOP CAN BE EASY

Abstraction and interface

interface it is a blueprint for the class, it is just like the contract we put some condition we can follow in the class.
Some Tips about interface :
all methods are abstract as nobody content for function.
All methods declared in an interface must be public;
We cannot define property in the interface;
this is simply the nature of an interface. Here is the example :

interface Logger
{
    public function execute();
}
Enter fullscreen mode Exit fullscreen mode

how can I use interface ?

we use interface by "implements" keyword as Example

class LogToFile implements Logger 
{
    public function execute($message) 
    {
        var_dump('log the message to a file :'.$message);
    }
}
Enter fullscreen mode Exit fullscreen mode

Abstract:
An abstract class is a class that contains at least one abstract method, which is a method without any actual code in it, just the name and the parameters, and that has been marked as "abstract".

The purpose of this is to provide a kind of template to inherit from and to force the inheriting class to implement the abstract methods.

An abstract class thus is something between a regular class and a pure interface. Also interfaces are a special case of abstract classes where ALL methods are abstract
as example

abstract class AbstractClass
{
    // Force Extending class to define this method
    abstract protected function getValue();

    public function printOut() 
    {
        print $this->getValue() . "\n";
    }
}
Enter fullscreen mode Exit fullscreen mode

we can conclusion the abstract in these points

should have at least one abstract function
have a regular function with implement
we can define properties

how can I use abstract?

we use abstract by "extends" keywork

Polymorphism Using Abstract Classes and Interfaces

abstract class Template
{
    public function make()
    {
        return $this
            ->addHotWater()
            ->addSugar()
            ->addPrimaryToppings()
            ->addMilk();
    }

    protected  function  addHotWater()
    {
        var_dump('Pour Hot water into cup');
        return $this;
    }

    protected  function addSugar()
    {
        var_dump('Add proper amount of sugar');
        return $this;
    }

    protected function addMilk()
    {
        var_dump('Add proper amount of Milk');
        return $this;
    }

    protected abstract function addPrimaryToppings();
}
Enter fullscreen mode Exit fullscreen mode
class Tea extends Template
{
    public function addPrimaryToppings()
    {
        var_dump('Add proper amount of tea');
        return $this;
    }
}

$tea = new Tea();
$tea->make();

class Coffee extends Template
{
    public function addPrimaryToppings()
    {
        var_dump('Add proper amount of Coffee');
        return $this;
    }
}

$coffee = new Coffee();
$coffee->make();
Enter fullscreen mode Exit fullscreen mode

we implement polymorphism as this way we make one template and implement many types and reduce duplicated code .

Discussion (0)