Skip to content

0036: Interfaces: how and when.

02/06/2011

Hi,

I’ve been doing programmer work for about 2.5 years. When I was starting I hardly understood concepts of classes and methods – let’s say: objective programming. So I started to learn – I was reading books, portals, blogs, and almost
everything that might help me to become better programmer (or just programmer :)) Now, when I can look back on my long hard road out of no-programmer-hell 🙂 I can tell that most troublesome and hard to understand thing was conception
of the Interface. When I discuss with younger programmers I see that they don’t understand it well too.
I’d like to, in short post, try to explain general idea of what interface is and how it can be used.

As we know, “An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface”.

OK, one can say, so interface is a crippled abstract class that cannot contain method bodies? Yes, if we want to look from this point. Interface only defines what the class does, not how it handles it.

In C++, as there inheritance from multiple base classes is allowed, one can use abstract classes (classes with methods declared, but not implemented) to emulate interface. In C# and other more objective languages (I know that C++ zealots
will try to crucify me) class can inherit only from one base class, BUT it can IMPLEMENT (not inherit) many interfaces. So, for example, we can have something like in OLGAtherer:

public class Book : IEntity, IPrintable, INotifyPropertyChanged

Assume that IPrintable contains only one method: void Print(), and we have entities like: Book, Comic, Ebook, Journal that implement IPrintable, but don’t inherit from common base class. We add button Print in our UI and attach it to the method:

void Print(object itemToPrint)

How would body of this method look if our classes wouldn’t implement IPrintable?

{
	if(itemToPrint is Book)
		(itemToPrint as Book).Print();
	else if(itemToPrint is Comic)
		(itemToPrint as Comic).Print();
	.
	.
	.
	.
	else
		.....		
	
	
}

Here you can read why switch statement isn’t implemented in .NET.

What, when interface comes to play?

void Print(IPrintable itemToPrint)
{
	itemToPrint.Print();
}

Surprise, surprise 🙂
Interface defines contract that given object is able to fulfill. Client is sure that when he works with object that implements specific interface, he can demand specific actions from him. He doesn’t care HOW the actions will be done.
He only knows that they WILL be done.

So, two main (in my opinion) purposes of using interfaces:

  • One class is able to implement many interfaces (instead of inherit from only one base class)
  • Different kind of classes not connected by inheritance may be treated in one way

Today we’ve just finished – I wrote this short and incomplete notice (interfaces have many hidden “catches”) only to make a background to further posts that will describe design patterns. These posts will also extend this one, because
will show in examples how the interfaces are used in production. You can also check my previous posts that describe techniques based on interface usage

Best regards,
Paweł

Advertisements
Leave a Comment

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: