Web programming

Interface in Object-Oriented Programming (OOP)

Ngoc Phuong

Ngoc Phuong

05-03-2021 . 42 view

I. Understanding Interface in Object-Oriented Programming (OOP)

1. What is an Interface?

An Interface in object-oriented programming is a template that allows you to specify which methods a class must implement.

Interfaces facilitate the use of different classes in the same manner. When one or more classes use the same Interface, it is referred to as polymorphism.

To declare an interface in PHP, we use the interface keyword.

Example

interface Person {
  public function getInfo();
  public function getInfo($name, $gender);
  public function getInfo() : string;
}

2. How to Use an Interface

To implement an Interface, a class must use the implements keyword.

A class that implements an Interface must implement all of its methods.

Example

interface Animal {
  public function makeSound();
}

class Cat implements Animal {
  public function makeSound() {
    echo "Meow";
  }
}

$animal = new Cat();
$animal->makeSound();

3. Characteristics of an Interface

An Interface is not an object, hence you cannot instantiate an Interface.

Example

interface Animal
{
    //
}

//FALSE because the interface cannot be initialized
$dog = new Animal();

In an Interface, you can only declare methods, but you cannot define them.

Example

interface Animal
{
    // CORRECT because we only declare methods in an interface
    public function setName();

    // INCORRECT because we cannot define methods in an interface
     public function getName()
     {
         //
     }
}

In an Interface, you can declare constants but cannot declare variables.

Example

interface Animal
{
    // CORRECT because constants can be declared in an interface
    const AGE = 100;

    // INCORRECT because variables cannot be declared in an interface
    // public $name;

}

Classes that implement an Interface must declare and define all the methods present in that Interface.

Example

interface Animal
{
    public function getName();
}

// This class is CORRECT because it has declared and defined the getName() method from the interface
class Dog implements Animal
{
    private $name;

    public function getName()
    {
        return $this->name;
    }
}

// This class is INCORRECT because it has not declared and defined the getName() method from the interface
class Cat implements Animal
{
    private $name;

    public function setName($name)
    {
        $this->name = $name;
    }
}

A class can implement multiple interfaces.

Example

interface Animal
{
    public function getName();
}

interface Lion
{
    public function checkDie();
}

class Tiger implements Animal, Lion
{
    private $name;
    const IS_DIE = false;

    public function getName()
    {
        return $this->name;
    }

    public function checkDie()
    {
        return self::IS_DIE;
    }
}

Interfaces can inherit from each other.

Example

interface Animal
{
    public function getName();
}

interface Lion extends Animal
{
    public function checkDie();
}

class Tiger implements Lion
{
    private $name;
    const IS_DIE = false;

    public function getName()
    {
        return $this->name;
    }

    public function checkDie()
    {
        return self::IS_DIE;
    }
}

II. Comparing Interface and Abstract Class

1. Differences Between Interface and Abstract Class

  • Interfaces cannot have properties, while abstract classes can.
  • All methods in an Interface must be public, while methods in an abstract class can be public or protected.
  • All methods in an Interface are abstract, meaning they cannot contain implementation but can only be declared.
  • A class can implement an Interface while also extending from another class.

Example

// We have interface Animal
interface Animal {
  const AGE = 10; // CORRECT
  // public $name; // INCORRECT
  public function getName(); // CORRECT
  // protected function setName(); // INCORRECT
  // public function getInfo()  // INCORRECT
  // {
  //   //
  // }

}

// We have an abstract class Animal
abstract class Animal {
  const AGE = 10; // CORRECT
  public $name; // CORRECT 
  abstract public function getName(); // CORRECT
  abstract protected function setName(); // CORRECT
  public function getInfo()  // INCORRECT, should be either abstract or have a body
  {
    //
  }

}

2. When to Use an Abstract Class and an Interface?

An abstract class is usually used when the inheriting classes share a common nature (belong to a group of objects).

An Interface is typically used when the inheriting classes do not share a common nature (group of objects) but can perform similar actions.

Ngoc Phuong
Ngoc Phuong

Web Developer

Thank you for visiting my website. My name is Ngoc Phuong, and I have over 10 years of experience in website development. I am confident in stating that I am an expert in creating impressive and effective websites. If you need a website designed, please feel free to contact me via email at [email protected].

0 feedback