Skip to content

Instantly share code, notes, and snippets.

@saji89
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saji89/a2667678a9d4382bfa40 to your computer and use it in GitHub Desktop.
Save saji89/a2667678a9d4382bfa40 to your computer and use it in GitHub Desktop.
Abstract class vs Interface (In PHP)
Abstract Class Interface
A method must be declared as abstract. Abstract methods doesn't have any implementation All methods, by default are abstract methods only. So, one cannot declare variables or concrete methods in interfaces
Abstract methods can be declared with access modifiers like public, private, protected. When implementing in subclass these methods must be defined with the same visibility All methods in an interface must be declared public
Abstract class can contain variables and concrete methods Interface cannot contain variables and concrete methods, except constants
A class can inherit only one abstract class ( multiple inheritance is not possible ) A class can implement, many interfaces ( multiple inheritance is possible )

e.g:

Abstract class

abstract class MotorVehicle
{
    int fuel;
    int getFuel()
    {
        return this.fuel;
    }
    abstract void run();
}

class Car extends MotorVehicle
{
    void run()
    {
        print("Vroom");
    }
}

Interface

interface MotorVehicle
{
    int getFuel();
    void run();
}

class Car implements MotorVehicle
{
    int fuel;
    
    void run()
    {
        print("Vroom");
    }

    int getFuel()
    {
        return this.fuel;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment