views:

895

answers:

4

Hello all,

What exactly is the difference between an interface and abstract class?

+12  A: 

An explanation can be found here: http://www.developer.com/lang/php/article.php/3604111/PHP-5-OOP-Interfaces-Abstract-Classes-and-the-Adapter-Pattern.htm

An abstract class is a class that is only partially implemented by the programmer. It may contain one or more abstract methods. An abstract method is simply a function definition that serves to tell the programmer that the method must be implemented in a child class.

An interface is similar to an abstract class; indeed interfaces occupy the same namespace as classes and abstract classes. For that reason, you cannot define an interface with the same name as a class. An interface is a fully abstract class; none of its methods are implemented and instead of a class sub-classing from it, it is said to implement that interface.

Anyway I find this explanation of interfaces somewhat confusing. A more common definition is: An interface defines a contract that implementing classes must fulfill. An interface definition consists of signatures of public members, without any implementing code.

Konamiman
+1 Thanks for the info :)
Mahesh Velaga
This is the most correct answer, since PHP interfaces differ from other languages in that PHP interfaces ARE abstract classes under the hood, whereas other languages' interfaces are signatures that classes must match. They behave the same as long as there are no errors though.
Tor Valamo
True, for PHP it's the real best anwser. But it's harder to get from the text blob than from a simple snippet.
e-satis
+10  A: 

Interface

An interface is a contract: the guy writing the interface say "hey, I accept things looking that way", and the guy using the interface say "OK, the class I write looks that way".

And interface is an empty shell, there are only the signatures (name / params / return type) of the methods. The methods do not contain anything. The interface can't do anything. It's just a pattern.

E.G (pseudo code):

// I say all motor vehicles should look like that :
interface MotorVehicle
{
    void run();

    int getFuel();
}

// my team mate complies and write vehicle looking that way
class Car implements MotoVehicle
{

    int fuel;

    void run()
    {
        print("Wrroooooooom");
    }


    int getFuel()
    {
        return this.fuel;
    }
}

Implementing an interface consume very little CPU, because it's not a class, just a bunch of names, and therefor there is no expensive lookup to do. It's great when it matters such as in embedded devices.

Abstract classes

Abstract classes, unlike interfaces, are classes. There are more expensive to use because there is a lookup to do when you inherit from them.

Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them. It's more about a guy saying "these classes should look like that, and they got that in common, so fill in the blanks !".

e.g:

// I say all motor vehicles should look like that :
abstract class MotorVehicle
{

    int fuel;

    // they ALL have fuel, so why let others implement that ?
    // let's make it for everybody
    int getFuel()
    {
         return this.fuel;
    }

    // that can be very different, force them to provide their
    // implementation
    abstract void run();


}

// my team mate complies and write vehicle looking that way
class Car extends MotorVehicule
{
    void run()
    {
        print("Wrroooooooom");
    }
}
e-satis
+1 for examples
nikc
nice explanation
stereofrog
practical explanation is always the best !
Sarfraz
+1  A: 

Not really the answer to the original question, but once you have the answer to the difference between them, you will enter the when-to-use-each dilemma: http://stackoverflow.com/questions/1231985/when-to-use-interfaces-or-abstract-classes-when-to-use-both

I've limited knowledge of oop, but seeing interfaces as an equivalent of an adjective in grammar has worked for me until now (correct me if this method is bogus!). For example, interface names are like attributes or cababilities you can give to a class, and a class can have many of them: ISerializable, ICountable, IList, ICacheable, IHappy, ...

azkotoki
+1  A: 

The key differences between an abstract class and an interface are:

  • Abstract classes can have consts, members, method stubs and defined methods, whereas interfaces can only have consts and methods stubs.
  • Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public.
  • When inheriting an abstract class, the child class must define the abstract methods, whereas an interface can extend another interface and methods don't have to be defined.
  • A child class can only extend a single abstract (or any other) class, whereas an interface can extend or a class can implement multiple other interfaces.
  • A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility.
Justin Johnson