DEV Community

Cover image for Interface And Abstract Class
Atakan Demircioğlu
Atakan Demircioğlu

Posted on

Interface And Abstract Class

My notes about interfaces and abstract classes with PHP examples.

What is an interface?

In very basic words, it is a contact or signature. Any class that implements an interface must be expected to have the same behavior. That means if you define a method contact that takes two params and returns a string, the class that implemented this interface must take the same behavior.

Let us discuss with an example;

<?php

interface FlyingCreature
{
    public function fly();
}

interface FeatheredCreature
{
    public function molt();
}

interface SwimmingCreature
{
    public function swim();
}

class Eagle implements FeatheredCreature, FlyingCreature
{
    public function fly()
    {
        // ...
    }

    public function molt()
    {
        // ...
    }
}

class Penguin implements FeatheredCreature, SwimmingCreature
{
    public function molt()
    {
        // ...
    }

    public function swim()
    {
        // ...
    }
}
Enter fullscreen mode Exit fullscreen mode

Firstly it is a good example of the interface segregation principle (ISP) and we can discuss this principle in another article.

  • In the example, you can see we use interface keywords to define an interface.
  • All methods must be public in an interface
  • For implementing an interface you need to use the implements keyword.
  • In PHP and in many programming languages you can just extend one class. For interfaces, you can implement more than one interface.
  • An interface can extend another interface with extends keywords.

What is Concrete Class?

A concrete class is a class that we can create an instance of, using the new keyword.

What are the advantages of using Interfaces?

  • The ability to define behavior that can be implemented by a group of unrelated classes without forcing them to share a common class hierarchy
  • As a class is not limited to the number of interfaces that it can implement, it can therefore participate in a wide variety of roles
  • You can modify existing systems to provide new functionality within their current class hierarchy
  • The class hierarchy can be simplified when developing with interfaces, which typically reduces overloading

What is Abstract Class?

It basically defines an interface for other classes to extend but has some differences.

  • You should add abstract keywords for defining an abstract class
  • An abstract class can have props and methods like a regular class.
  • Abstract class can't be instantiated.
  • If a class contains at least one abstract method, it must be an abstract class.
  • A class that extends an abstract class must implement all abstract methods, not regular methods. This part is different from an interface.

Here is a basic example of usage;

<?php

abstract class Debugger
{
    abstract public function debug($message);
}

class BrowserDebugger extends Debugger
{
    public function debug($message)
    {
        echo "<pre>";
        print_r($message);
        echo "</pre>";
    }
}

class CLIDebugger extends Debugger
{
    public function debug($message)
    {
        print_r($message);
    }
}

$debugger = new CLIDebugger();
$debugger->debug(array('foo' => 'bar'));

$debugger = new BrowserDebugger();
$debugger->debug(array('foo' => 'bar'));
Enter fullscreen mode Exit fullscreen mode

Differences with Abstract Class and Interface

  • One class can extends just one abstract class but more than one interface.
  • In abstract classes, you can define filled methods but in the interface is not possible.
  • The interface supports multiple inheritances but abstract classes do not support them.
  • In interfaces, all methods must be public but in abstract class is not a must.
  • Abstract classes can have a constructor but interfaces can't.
  • In interfaces, you can't define static methods.

This article orginally published in medium.

Top comments (0)