Interface Example In C#

In our previous article, we discussed Abstract Class which is used as an architectural pillar while building an application. Same ways interface is also used to define an architecture for the application. In this article we will learn about Interfaces, What are these? How we use them?


interfaces in c#




What Is An Interface?


Interfaces are entities which define a set of functionalities which are mandatory to implement for classes which are inheriting from that interface. Let's have a look, how the interface looks like

interface IFruit
{
    //Property
    string Description { get; set; }

    //Methods
    string Name();
    string Season();
    string Color();
}

Shown above is an example of an interface named as IFruit. It's a convention to append letter 'I' before the name of an interface. As you can notice that before IFruit we have written interface keyword to let the compiler understand that we are declaring an interface. We don't declare variables inside interfaces and every Method or property is considered public and we can't use access modifiers.

Inside the Interfaces, Methods and Properties are only declared. We can't write functionality inside interfaces. The functionality or definition is written inside the classes that implement the interface.

How To Implement Interface?


 class Orange : IFruit
    {

        public string Description
        {
            get;set;
        }

        public string Name()
        {
            return "Orange";
        }

        public string Season()
        {
            return "Winters";
        }

        public string Color()
        {
            return "Orange";
        }
    }


Now in the above code, we have a class name as Orange. This class is implementing the IFruit interface using ':' sign like we use in the inheritance of class.
In order to implement, this class has to provide a definition for all the methods and properties declared inside the interface. That's how interfaces enforce a set of functionality inside a class.

If you miss out any field while defining the implementation, then compiler throws an error. For example, if we miss providing definition of Description property in Orange class then compiler will say
'Orange' does not implement interface member 'IFruit.Description'

You can implement multiple interfaces inside a class, just add comma next to your interface as shown below

Class : Interface1,  Interface2,  Interface3, ...


Summary Of An Interface

  • Not a class, it’s an entity which enforces set of functionalities to be present.
  • The convention is that interface name should start with ‘I’ prefix.
  • A class can inherit from multiple interfaces.
  • Can’t initialize variables.
  • Can’t write an implementation in interfaces. Just declarations
  • A class inheriting from an Interface has to implement all its properties and methods, else compiler throws an error.
  • An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public

Hope you understood what exactly Interfaces are. Let us know your queries. We are happy to help.
Happy Learning!! Follow our full C# Tutorial Index for complete series.


Abstract Class vs Interface: Video Below





Comments



  1. Hello sir

    I watch ur video on youtube
    How implement infinite scroll

    I am using a firebase recycler adapter how to add infite scroll to it

    ReplyDelete

Post a Comment

Hey there, liked our post. Let us know.

Please don't put promotional links. It doesn't look nice :)

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example