Interfaces

Self-Innovator
Posted by in OOPS category on for Intermediate level | Points: 250 | Views : 1692 red flag

In this article we will going to see in depth about Interfaces in C#. Interface are the key concept on every programming.

Introduction

Interface plays a vital role in a real world of programming, It is an key concept on OOPS mechanism because it provides a high end flexibility on programming.


Background

Interface is basically a class which is widely useful in programming. Interface has a basic syntax on it's declaration and usage. It provides high end features on all applications which gives much flexibility on achieving the results and hence it is much needed to all developers.

Using the code

Now we will try to look the different scenarios of Interfaces and how it can be useful during programming.

Scenario 1: All the members of Interface class are public by Default

Interface are basically a class which can have only the method definitions, it doesn't allow any implementations with in its body. All the members of an interface class should be public by default,because interfaces should be inherited by the derived classes Unlike the abstract class interfaces cannot be instantiated. In the below code snippet we have used two interfaces namely IGSMMobile and IAndroidMobile in which we are declaring the members of an interfaces with access modifiers as prefix keyword public during the declaration which results in a compile time error.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacesDemo
{
    interface IGSMMobile
    {
        public void Calling();
        public void Messaging();
    }
    interface IAndroidMobile
    {
        public void VideoCalling();
        public void WhatsApp();
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}


Scenario 2: Interface cannot have implementations

Interface can have only the declarations, unlike a normal classes and abstract classes it doesn't allow implementations. The implementation for the interface method should be implemented by derived classes. In the below code snippet the IGSMMobile interface has it's method implementation which results in compile time error. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacesDemo
{
    interface IGSMMobile
    {
        void Calling()
        {
            Console.WriteLine("Nokia 1100 has calling features");
        }
        void Messaging()
        {
            Console.WriteLine("Nokia 1100 has messaging features");
        }
    }
    interface IAndroidMobile
    {
        void VideoCalling();
        void WhatsApp();
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

 
Scenario 3: All the methods of interface must be implement by derived class

All the methods of an interface class must be implement by it's derived class which is mandatory. Unlike the abstract class the interface doesn't allow to have methods with out it's implementation once it is being inherited. In the below code snippet a derived class (Nokia1100) inherited an interface (IGSMMobile) and doesn't implement it's in one of the method (Messaging) which results in compile time error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacesDemo
{
    interface IGSMMobile
    {
        void Calling();
        void Messaging();
    }
    interface IAndroidMobile
    {
        void VideoCalling();
        void WhatsApp();
    }
    class Nokia1100 : IGSMMobile
    {
        public void Calling()
        {
            Console.WriteLine("Nokia 1100 has calling features");
        }        
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}


Scenario 4: Multiple Interface Inheritance

Multiple inheritance is supporting by interface similar to normal classes supports. In the below code snippet we are trying to inherit two interfaces (IGSMMobile) and (IAndroidMobile) by a derived class Nokia1100 which results in compile time error due to all methods of interface (IAndroidMobile) class is not implemented
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacesDemo
{
    interface IGSMMobile
    {
        void Calling();
        void Messaging();
    }
    interface IAndroidMobile
    {
        void VideoCalling();
        void WhatsApp();
    }
    class Nokia1100 : IGSMMobile,IAndroidMobile
    {
        public void Calling()
        {
            Console.WriteLine("Nokia 1100 has calling features");
        }
        public void Messaging()
        {
            Console.WriteLine("Nokia 1100 has calling features");
        }
      
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}


Scenario 5: Derived class must implements all methods

A derived class must implements all the methods of base interface class which is mandatory, In the below code snippet a tow derived classes (Nokia1100) and (Samsung S5) are implementing all methods of a base interface class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfacesDemo
{
    interface IGSMMobile
    {
        void Calling();
        void Messaging();
    }
    interface IAndroidMobile
    {
        void VideoCalling();
        void WhatsApp();
    }
    class Nokia1100 : IGSMMobile
    {
        public void Calling()
        {
            Console.WriteLine("Nokia 1100 has calling features");
        }
        public void Messaging()
        {
            Console.WriteLine("Nokia 1100 has calling features");
        }      
    }
    class SamsungS5 : IAndroidMobile, IGSMMobile
    {
        public void VideoCalling()
        {
            Console.WriteLine("Samsung S5 has video calling features");
        }
        public void WhatsApp()
        {
            Console.WriteLine("Samsung S5 has whatsapp messaging features");
        }
        public void Calling()
        {
            Console.WriteLine("Nokia 1100 has calling features");
        }
        public void Messaging()
        {
            Console.WriteLine("Nokia 1100 has calling features");
        }   
    }
    class Program
    {
        static void Main(string[] args)
        {
            Nokia1100 N = new Nokia1100();
            Console.WriteLine("\n Nokia 1100 features");
            Console.WriteLine("***********************");
            N.Calling();
            N.Messaging();

            SamsungS5 S = new SamsungS5();
            Console.WriteLine("\n Samsung S5 features");
            Console.WriteLine("************************");
            S.Calling();
            S.Messaging();
            S.VideoCalling();
            S.WhatsApp();N.Calling();

            Console.ReadKey();
        }
    }
}


Conclusion

In this article we have seen various situations of an interface class and how it can be useful when used in our applications.

Reference

http://tempuri.org/tempuri.html

Page copy protected against web site content infringement by Copyscape

About the Author

Self-Innovator
Full Name: Sayeed Ahmed
Member Level: Bronze
Member Status: Member
Member Since: 12/22/2011 7:45:35 AM
Country: India
Join Hands Change lives Thanks & Regards Straight Edge Society


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)