My contemplations and diurnal novelties are publicized here


Interfaces

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

An 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 MotorVehicle

{

 

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 MotorVehicle

{

void run()

{

print(“Wrroooooooom”);

}

}

Implementation

While abstract classes and interfaces are supposed to be different concepts, the implementations make that statement sometimes untrue. Sometimes, they are not even what you think they are.

In Java, this rule is strongly enforced, while in PHP, interfaces are abstract classes with no method declared.

In Python, abstract classes are more a programming trick you can get from the ABC module and is actually using metaclasses, and therefore classes. And interfaces are more related to duck typing in this language and it’s a mix between conventions and special methods that call descriptors (the __method__ methods).

As usual with programming, there is theory, practice, and practice in another language 🙂

 

Copied from http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: