HowTo: Understanding Interfaces – a simple description

image Interfaces are an important feature for designing great software, but many programming newbies have a understanding problem – Why should I use "interfaces"? What is an "interface"?

 

A very simple sample
I would like to create 3 types (Train, Car, Human) in my (very simple, not real world) sample. Each type is movable and that´s why I want to create the "IMovable" interface. 
To move these types I implement a  "God" class which can move these objects as he wishes. (I know – it´s a very real sample ;) ).

Structur:

image

The most important thing is our "IMovable" interfaces (the name of an interface begins with "I" in .NET):

    public interface IMovable
    {
        void Move();
    }

You define methods, properties or events in the interface – it´s a kind of a contract and the real implementation doesn´t matter.

    public class Human : IMovable
    {
        #region IMovable Member

        public void Move()
        {
            Console.WriteLine("The human take a step.");
        }

        #endregion
    }

Our human implements the interface – and "take a step" to "Move". The signature of the methods must be equal to the method of the interface (parameters, return value)!

Implementing god

Now it´s time to move! We implement now our God class with the "MoveObject" method. It takes an item which implement the "IMovable" interface:

    public class God
    {
        public static void MoveObject(IMovable item)
        {
            Console.WriteLine("God moves something...");
            item.Move();
        }
    }

The method "MoveObject" takes anything that implements the interface – the concret type doesn´t matter! You can now put a car, train, human or another type in it – as long as it implements the "IMovable" interface!

image

Now we can create many classes which implement the interface and we don´t need to change anything in our God class!

Our test program:

    class Program
    {
        static void Main(string[] args)
        {
            Car BMW = new Car();
            Train ICE = new Train();
            Human Robert = new Human();

            God.MoveObject(BMW);
            God.MoveObject(ICE);
            God.MoveObject(Robert);

            Console.ReadLine();
        }
    }

We have a train, a BMW and me – the result (the original source code was in german, I just translated it in this post) :

image

[ Download Source Code ]

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Currently there is no additional info about this author.

One Response

Comment on this post

Recent Posts

  • image1528-570x194_thumb.png
    Introduction to Redis on Windows & Redis usage with .NET

      Redis belongs to the NoSQL data banks and you will find it in the group of Key-Value Stores. Redis is often named “Blazing Fast” and according to the Stackoverflow Thread it is used to be two time (while writing) and three times (while reading) quicker than MongoDB. Even if the comparison is a little ...

  • Automated Security Analyser for ASP.NET websites

    Evil Hackers are lurking everywhere and many Web-applications are delicately and share “too much” with the attacker. A quick (first!) overview offers the Tool “ASafaWeb”. All the website does is making a few requests and writing an Analyses including problem solving’s. There are no permanent disadvantages (bad requests/ DoS attacks and so on). Example: KnowYourStack.com ...

  • image1489-570x194.png
    „Sign in with Twitter“ for your own ASP.NET WebApp

      “Sign in with Twitter” is a popular practice to authenticate the users on your website. One advantage compared to an own registration is the lower inhibition for the user. But on the other hand Twitter doesn’t fess up with all the information’s and you will get into a kind of addiction. At the end ...

  • image1485-570x194_thumb.png
    CodePlex is going to be updated

      CodePlex the Microsoft Open Source Project Hosting Plattform hasn’t changed that much in the last few years and for a few times I thought Microsoft stopped the whole developing process. But now I found out that there is still life in the project. Maybe it is because of the success of GitHub or because ...

  • image1474_thumb.png
    What does Adobe in the flash-free web? Magazine-Style Layouts with CSS Regions!

      Adobe is well known for Photoshop and Flash but of course there is a lot more. According to the “Future Post” from Google Adobe declared one of their big subjects on a Blogpost. I’m talking about the W3C Working Draft to CSS Regions. Adobe cooperates with the WebKit Team and W3C on this. What ...

Support us