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

  • image1452_thumb.png
    Javascript to Dart Translator

      Dart, a Google Javascript alternative was presented a few months ago and the web developer scene are a little bit unsure about the usability of Dart. To declare the language Google has translated the Javascript basics into Dart. The result is this “Translator”. In my opinion the name doesn’t find that well because it’s ...

  • image1366-570x194.png
    Twitter Bootstrap as UI-kit

      HTML and CSS are not foreign words for me but I regret, I’m not a Web designer – I see myself as a webdeveloper. But at least a dressy side is a must. But thank good there are some ready “Systems”. Twitter Bootstrap Twitter Bootstrap is a Toolkit for every kind of Web applications. ...

  • image1441.png
    Fix: the value ‚x‘ is not valid for Foo in ASP.NET MVC

      To get files into the MVC Controller Modelbinding from MVC is a clever method. But in fact it is a little bit complicated to set the error message if the connection failed. Example: public class RegisterModel { ... [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email address")] public string Email { get; set; } [Required] [Display(Name = ...

  • You Tube API – recall Video Meta files with .NET

      A loooong time ago I’ve blogged about how to access to You Tube with the Google Data APIs. After all that time there are several new ways how to recall files. Google offers You Tube a “simple” surface. If you prefer to do low-Level HTTP calls it is also possible. Aim: I want the ...

  • image1426-570x194.png
    MacBook Pro for .NET Developer – useful ore just pretty?

      I own a MacBook Pro (from 2010) for about a year now and because I’ve used to think about this Question since I have it, I’m going to blog about my experience now. My Notebook Configurations MacBook Pro April 2010 - 2,66 Intel Core i7 - 8GB RAM - 15’’ Glossy Display - Intel ...

Support us