Please enable Javascript for better experience...
An introduction to Interface in C#
By Rahul Kumar Jha | Nov 29, 2014 | In Tutorials | Update: Mar 20, 2018 | Total Views [ 3263 ]
Taged In
(0 Like)
Rate

In this tutorial we will learn about Interface and it's use in OOP.

Introduction

Interface is forcefully implementation of contract which contains only signature of methods, properties, events or indexers. Class or struct which inherits interface must implement all its members. In simple words, we can say that an Interface is the contract between two parties. It can be used to achieve runtime polymorphism. Using polymorphism, it can be decided at run time that which class to be used with help of same interface. We will discuss this in upcoming chapters.

Public scope of Interface

We can create interface using interface keyword and by default its scope is public. All the members defined in interface are public in nature. The reason behind its public scope is simple as interface is defined to communicate between two parties. We usually write  business logics in a class, create dll and expose main functionalities to client with the help of interface and that's why interface is kept public by default.

Use of Interface

An Interface can be used for many purpose like to provide a roadmap to developer, or to share a contract with client. It can also be used to achieve run time polymorphism. It confirms implementation of all the members defined in it.

Creating Interface

Below code snippet has an interface with properties and methods defined in it.

public interface IA
{
/// <summary>
/// Property ID of integer type
/// </summary>
int ID { get; set; }
/// <summary>
/// Display method
/// </summary>
void Display();
}

By default an interface and the definitions inside it are public. You can inherit one interface from other interface too.

Implement Interface in Class

Below snippet shows how to implement an Interface in your class. You must implement all the methods, properties defined in interface in your class. In below example class ClassA inherits interface IA and it must implement the property ID and method Display() to compile successfully else it will throw compile time error.

public class ClassA:IA
{
public int ID { get; set; }
public void Display()
{
//TODO: Your Code
}
}

Call methods in program

Let's call the method in program with the help of interface. In below code snippet, class Program has a Main() method which creates object of class ClassA with the help of interface IA and later it calls Display() method.

class Program
{
static void Main(string[] args)
{
// Declare an interface instance.
IA obj = new ClassA();
// Call the member.
obj.Display();
}
}

Let's see one more example. In below example we will inherit parent interface to child interface. Once one child interface inherits parent interface, the class which inherits this child interface, must need to implement all methods or properties from parent as well as child interface. We will see this in below example.

Define Parent-Child interface

Let's define two interface IA and IB and inherit interface IA into interface IB.

interface IA
    {
        void WelcomeToDNC();
    }

    interface IB: IA
    {
        void Display();
    }

Inherit interface in class

In above code snippet, we defined two interface IA and IB where IB inherits IA. Let's define a class ClassA which inherits from interface IB. Now remember as I said earlier, this class must implement methods from interface IA as well as IB. Let's see below:

/// <summary>
    /// This class ClassB must implement methods declared in interface IA as well as in IB
    /// </summary>
    class ClassB: IB
    {
        void WelcomeToDNC()
        {
            //TODO:
        }

        void Display()
        {
            //TODO:
        }
    }

Creating object of class through interface

We can use interface to achieve Run time Polymorphism deciding which methods to be accessed during run time. If we create object of ClassB normally as ClassB obj = new ClassB(), you will not see any difference. But if you want some access restriction, security or easy maintainbility and feasibility of your code, better you create object of class using interface. In below example, we created object of class ClassB separately one by one with interface IA and IB. Once we created object using interface IA, it will only allow to access method WelcomeToDNC() as interface IA is super interface or parent interface.

implement 1

When we created object of class ClassB using interface IB, we were able to access both methods i.e. WelcomeToDNC() from parent interface IA and Display() from child interface IB.

implement 1

Hope above examples helps you understanding the concept of interface and its uses.

<< Class and Object in C#

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer dotnet-concept.com

Public profile: user/profile/99900001


Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments


 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment
 
 
 
 
 
 
Sponsors