Why use interfaces

This a general overview about what are the advantages for using interfaces in C#. Here are some the many reason fro which we should consider using an interface while development.

  • To make use of multiple inheritance
  • C# does not allow multiple class inheritance. We can derive from only one class at a time, but not more. We can indeed inherit our class, interface from multiple interfaces.

    public class ClassB : ClassA, InterFaceA, InterFaceB
    {
     //
    }
    
  • If we expect similar behaviour from many classes, it is better to inherit them from an Interface. This makes it easy for plug-in style development and future extension of components.
  • Interfaces provide flexibility and support for Unit Testing. Most of the Testing Frameworks rely on Interfaces for Mock objects , which are created and provided with the dependencies inside the Test code Block. Instead of creation from classes, these Mocking frameworks need an Interface.
  • Interface allow us maximum flexibility for the adoption of change, without the much headache and breaking builds.
  • Interfaces are more flexible than base classes. We can define a single implementation that can implement multiple interfaces.
  • Inversion of control and dependency Injection totally rely over interfaces.
  • A very important building unit, Struct, cannot inherrit from classes but can inherit from an Interface.
  • Consider this example:
    You have a class that is capable of playing media files(mp3). You give that class to you friend who tries to play MPEG type of files. It would not be possible for him to do so without making significant changes to you class.

    public class MusicPlayer 
    {
       void Play(Mp3 _mp3File){}
    }
    

    Now consider this
    Instead of passing type of mp3 file to Play Method what if you pass this Method, a derived from an interface of Type MediaType.

    public interface MediaType
    { }
    
    public class Mp3 : MediaType
    { }
    
    public class MPEG : MediaType
    { }
    
    public class MusicPlayer 
    {
       void Play(MediaType _mediaFile){}
    }
    

    In this scenario, you can derive another MediaFile type and from MediaType like MPEG and pass that to the Play Method and it will happily accept it and play it for you (provided logic).

    public class TestPlayers
        {
            public void PlayMedia()
            {
                MusicPlayer musicPlayer = new MusicPlayer();
                musicPlayer.Play(new Mp3());
                musicPlayer.Play(new MPEG());
            }       
        }
    

    for reference and more information:

  • Significance of Interfaces C#
  • 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