All Projects → nahidulhasan → Oop

nahidulhasan / Oop

Interface and abstract class - simple and easy explanation

Projects that are alternatives of or similar to Oop

Java-Programs
Java Practiced Problems including concepts of OOPS, Interface, String , Collection.
Stars: ✭ 51 (-53.21%)
Mutual labels:  interface, oop
Softuni
SoftUni Courses
Stars: ✭ 98 (-10.09%)
Mutual labels:  oop
Designpatterns
DesignPatterns samples by csharp on dotnetcore 《大话设计模式》 中设计模式总结/C#(.NETCore)代码
Stars: ✭ 73 (-33.03%)
Mutual labels:  oop
Lane Detection For Autonomous Cars
Lane Detection Module using C++ and OpenCV
Stars: ✭ 90 (-17.43%)
Mutual labels:  oop
Vtkfortran
pure Fortran VTK (XML) API
Stars: ✭ 79 (-27.52%)
Mutual labels:  oop
Python Interface
Minimal Pythonic Interface Definitions
Stars: ✭ 92 (-15.6%)
Mutual labels:  interface
Secure If
Android USB Control app
Stars: ✭ 68 (-37.61%)
Mutual labels:  interface
Wp Rest Starter
Starter package for working with the WordPress REST API in an object-oriented fashion.
Stars: ✭ 105 (-3.67%)
Mutual labels:  oop
Shellfuncs
Python API to execute shell functions as they would be Python functions
Stars: ✭ 96 (-11.93%)
Mutual labels:  interface
Elvui
ElvUI for World of Warcraft - The Burning Crusade (2.4.3)
Stars: ✭ 89 (-18.35%)
Mutual labels:  interface
Elegantobjects.github.io
Fan club for Elegant Objects programmers
Stars: ✭ 89 (-18.35%)
Mutual labels:  oop
Iced
A cross-platform GUI library for Rust, inspired by Elm
Stars: ✭ 12,176 (+11070.64%)
Mutual labels:  interface
Off
OFF, Open source Finite volume Fluid dynamics code
Stars: ✭ 93 (-14.68%)
Mutual labels:  oop
Excel Io
Object-oriented java Excel library
Stars: ✭ 76 (-30.28%)
Mutual labels:  oop
Framy Css
Very simple CSS Framework
Stars: ✭ 103 (-5.5%)
Mutual labels:  interface
Ara
ARA Records Ansible and makes it easier to understand and troubleshoot.
Stars: ✭ 1,176 (+978.9%)
Mutual labels:  interface
Infinity
A lightweight C++ RDMA library for InfiniBand networks.
Stars: ✭ 86 (-21.1%)
Mutual labels:  interface
Filebrowser
📂 Web File Browser
Stars: ✭ 13,854 (+12610.09%)
Mutual labels:  interface
Shestakui classic
ShestakUI for WoW Classic (1.13.2)
Stars: ✭ 108 (-0.92%)
Mutual labels:  interface
Dev Stuff
😎 Programming stuff for everyone. Collection of articles, videos about architecture, Domain Driven Design, microservices, testing etc.
Stars: ✭ 105 (-3.67%)
Mutual labels:  oop

Welcome to OOP

Interface

We know that an interface is defined by the interface keyword and all methods are abstract. All methods declared in an interface must be public; this is simply the nature of an interface. Here is the example :

<?php
interface Logger
{
    public function execute();
}

In an interface the method body is not defined, just the name and the parameters. If we do not use an interface what problems will happen? Why we should use interface? Herein you will get these answers. Please see the code in below:

<?php
class LogToDatabase 
{
    public function execute($message)
    {
        var_dump('log the message to a database :'.$message);
    }
}

class LogToFile 
{
    public function execute($message)
    {
        var_dump('log the message to a file :'.$message);
    }
}

class UsersController 
{ 
    protected $logger;
    
    public function __construct(LogToFile $logger)
    {
        $this->logger = $logger;
    }
    
    public function show()
    { 
        $user = 'nahid';
        $this->logger->execute($user);
    }
}

$controller = new UsersController(new LogToFile);
$controller->show();

In the above example I do not use interface. I write to the log using the LogToFile class. But now if I want to write a log using LogToDatabase I have to change hard coded class reference in the above code on line number 23. That line code in below :

public function __construct(LogToFile $logger)

This code will be

public function __construct(LogToDatabase $logger)

In a large project, if I have multiple classes and a need to change, then I have to change all the classes manually. But If we use an interface this problem is solved; and we will not need to change code manually.

Now see the following code and try to realize what happened if I use interface:

<?php
interface Logger 
{
    public function execute($message);
}

class LogToDatabase implements Logger 
{
    public function execute($message){
        var_dump('log the message to a database :'.$message);
    }
}

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

class UsersController 
{
    protected $logger;
    
    public function __construct(Logger $logger) 
    {
        $this->logger = $logger;
    }
    
    public function show() 
    {
        $user = 'nahid';
        $this->logger->execute($user);
    }
}

$controller = new UsersController(new LogToDatabase);
$controller->show();

Now If I change from LogToDatabase to LogToFile I do not have to change the constructor method manually. In the constructor method I have Injected an interface; not any arbitrary class. So If you have multiple classes and swap from one class to another class you will get the same result without changing any class references.

In the above example I write a log using LogToDatabase and now I want to write log using LogToFile, I can call it in this way

$controller = new UsersController(new LogToFile);
$controller->show();

I get the result without changing other classes. Because the interface class handles the swapping issue.

Abstract class

An abstract class is a class that is only partially implemented by the programmer. It 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”.

An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class. Here is the example :

<?php
abstract class AbstractClass
{
    // Force Extending class to define this method
    abstract protected function getValue();
    
    public function printOut() 
    {
        print $this->getValue() . "\n";
    }
}

Now the question is when will the situation that a method will be needed and must be implemented? Here I will try to explain. Please see the Tea class.

<?php
class Tea 
{
    public function addTea()
    {
        var_dump('Add proper amount of tea');
        return $this;
    }
    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;
    }
    
    public function make()
    {
        return $this
            ->addHotWater()
            ->addSugar()
            ->addTea()
            ->addMilk();
    }
}

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

Now we look at the coffee class.

<?php
class Coffee 
{
    public function addCoffee()
    {
        var_dump('Add proper amount of tea');
        return $this;
    }
    
    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;
    }
    
    public function make()
    {
        return $this
            ->addHotWater()
            ->addSugar()
            ->addCoffee()
            ->addMilk();
    }
}

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

In the above two classes the three methods addHotWater(), addSugar(), and addMilk() are same. So we should remove duplicated code. We can do it in the following way :

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();
}

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();

I make an abstract class and name it Template. Here I define addHotWater(), addSugar() and addMilk(); and make an abstract method named addPrimaryToppings.

Now If I make the Tea class extend the Template class then I will get the three defined methods and must define the addPrimaryToppings() method. In a similar way the coffee class will as well.

I have published this article in the medium. if you’d like to read from the medium blog site, please go to this link

Thanks for reading.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].