Interface or an Abstract Class: which one to use? Interface or an Abstract Class: which one to use? php php

Interface or an Abstract Class: which one to use?


Use an interface when you want to force developers working in your system (yourself included) to implement a set number of methods on the classes they'll be building.

Use an abstract class when you want to force developers working in your system (yourself included) to implement a set numbers of methods and you want to provide some base methods that will help them develop their child classes.

Another thing to keep in mind is client classes can only extend one abstract class, whereas they can implement multiple interfaces. So, if you're defining your behavior contracts in abstract classes, that means each child class may only conform to a single contract. Sometimes this a good thing, when you want to force your user-programmers along a particular path. Other times it would be bad. Imagine if PHP's Countable and Iterator interfaces were abstract classes instead of interfaces.

One approach that's common when you're uncertain which way to go (as mentioned by cletus below) is to create an interface, and then have your abstract class implement that interface.


The differences between an Abstract Class and an Interface:

Abstract Classes

An abstract class can provide some functionality and leave the rest for derived class.

  • The derived class may or may not override the concrete functions defined in the base class.

  • A child class extended from an abstract class should logically be related.

Interface

An interface cannot contain any functionality. It only contains definitions of the methods.

  • The derived class MUST provide code for all the methods defined in the interface.

  • Completely different and non-related classes can be logically grouped together using an interface.


Why to use abstract classes? The following is a simple example. Lets say we have the following code:

<?php class Fruit {    private $color;    public function eat() {        // chew    }    public function setColor($c) {        $this->color = $c;    }}class Apple extends Fruit {    public function eat() {        // chew until core    }}class Orange extends Fruit {    public function eat() {        // peeling        // chew    }}

Now I give you an apple and you eat it.What does it taste like? It tastes like an apple.

<?php $apple = new Apple();$apple->eat();// Now I give you a fruit.$fruit = new Fruit();$fruit->eat();

What does that taste like? Well, it doesn't make much sense, so you shouldn't be able to do that. This is accomplished by making the Fruit class abstract as well as the eat method inside of it.

<?php abstract class Fruit {    private $color;    abstract public function eat(){}    public function setColor($c) {        $this->color = $c;    }}?>

An abstract class is just like an interface, but you can define methods in an abstract class whereas in an interface they are all abstract. Abstract classes can have both empty and working/concrete methods. In interfaces, functions defined there cannot have a body. In abstract classes, they can.

A real world example:

<?php abstract class person {    public $LastName;    public $FirstName;    public $BirthDate;    abstract protected function write_info();}final class employee extends person{    public $EmployeeNumber;    public $DateHired;    public function write_info(){        //sql codes here        echo "Writing ". $this->LastName . "'s info to emloyee dbase table <br>";       }}final class student extends person{    public $StudentNumber;    public $CourseName;    public function write_info(){        //sql codes here        echo "Writing ". $this->LastName . "'s info to student dbase table <br>";    }}///----------$personA = new employee;$personB = new student;$personA->FirstName="Joe";$personA->LastName="Sbody";$personB->FirstName="Ben";$personB->LastName="Dover";$personA->write_info();// Writing Sbody's info to emloyee dbase table$personB->write_info();// Writing Dover's info to student dbase table