What are interfaces


What are interfaces?

 

In this article I will be explaining you about interface, there types and implementation.

 

Definition

 

Interfaces are contracts between two disparate pieces of code. That is, once an interface is defined and a class is defined as implementing that interface, clients of the class are guaranteed that the class has implemented all methods defined in the interface.

 

When you define an interface and specify that a class is going to make use of that interface in its definition, the class is said to be implementing the interface or inheriting from the interface. Interfaces are defined behaviors and a class is defined as implementing that behavior.

 

An interface looks like a class, but has no implementation. In addition to methods and properties, interfaces can declare events and indexers as well.

 

Few points regarding interfaces:  

  • Interface only has method declaration i.e. method prototype and signature.
  • Interface doesn't contain any field.
  • Interface provide base layer of functionality in OOAD (Object Oriented Application Design).
  • No access modifier is used with interface (By default it is public).
  • No abstract, virtual, static, sealed or override keywords are used with interface.
  • Interface can inherit interface with any type of inheritance.
  • Constructor can't be used in interface.
  • In .NET interface is used to allow multiple and hybrid inheritance ambiguity problem.
  • Structs can also inherit interfaces.

Practical demonstration of interface implementation

 

using System;

 

namespace interface_implementation

{

    class Program

    {

        public interface aa

        {

            // no access specifier is given in interface methods (by defualt they are public)

 

           int sum(int a, int b);

            void display();

        }

 

        public class XX : aa

        {

 

            public int sum(int a, int b)

            {

                return (a + b);

            }

 

            public void display()

            {

                Console.WriteLine("This method is declared in interface");

            }

 

            public void show()

            {

                Console.WriteLine("This is method of class XX");

            }

        }

 

        static void Main(string[] args)

        {

            XX obj = new XX();

 

            Console.WriteLine("Method sum " + obj.sum(12, 12));

            obj.display();

            obj.show();

 

            // You can create reference of a interface

            // So we can say interface is public

 

            aa obj1;

            obj1.display();

 

            // You can't create a instance of a interface

            //aa obj2 = new aa(); // this will give error

            Console.ReadLine();

        }

    }

}

 

Practical demonstration of multiple interface implementation

 

using System;

 

namespace interface_1

{

    class Program

    {

        public interface aa

        {

            int sum(int a, int b);

            void display();

        }

 

        public interface bb

        {

            int sumt(int a, int b);

            void display();

        }

 

// here the class is inherting more than one interface i.e. multiple inheritance

        public class XX : aa, bb

        {

            public int sum(int a, int b)

            {

                return (a + b);

            }

 

            public int sumt(int a, int b)

            {

                return (a - b);

            }

 

            public void display()

            {

                Console.WriteLine("This method is declared in interface");

            }

 

            public void show()

            {

                Console.WriteLine("This is method of class XX");

            }

        }

 

        static void Main(string[] args)

        {

 

            XX obj = new XX();

 

            Console.WriteLine("Method sum " + obj.sum(12, 12));

            Console.WriteLine("Method substract " + obj.sumt(12, 2));

            obj.display();

            obj.show();

            Console.ReadLine();

        }

    }

}

 

Practical demonstration of interface inheriting interface and than class is inheriting interfaces.

 

using System;

 

namespace interface_2

{

    class Program

    {

        public interface aa

        {

            // no access specifier is given in interface methods (by defualt they are public)

 

            int sum(int a, int b);

            void display();

        }

 

        public interface bb : aa

        {

            // no access specifier is given in interface methods (by defualt they are public)

 

            int sumt(int a, int b);

        }

 

        public class XX : bb

        {

            public int sum(int a, int b)

            {

                return (a + b);

            }

 

            public int sumt(int a, int b)

            {

                return (a - b);

            }

 

            public void display()

            {

                Console.WriteLine("This method is declared in interface");

            }

 

            public void show()

            {

                Console.WriteLine("This is method of class XX");

            }

        }

 

        static void Main(string[] args)

        {

 

            XX obj = new XX();

 

            Console.WriteLine("Method sum " + obj.sum(12, 12));

            Console.WriteLine("Method subtract " + obj.sumt(12, 2));

            obj.display();

            obj.show();

 

            Console.ReadLine();

        }

    }

}

 

Interface can be implemented in two ways:

 

1. Implicit manner of interface (Class binding)

2. Explicit manner of interface (Type name binding)

 

In interface implementation we have to implement all interface methods into derived class otherwise derived type must be marked, as abstract and interface method must be declared with abstract keyword.

 

Interface methods are public so you can't use any access modifier even public.

 

Above practical code were examples of implicit implementation of interface.

 

Practical demonstration of Explicit interface implementation

 

using System;

 

namespace interface_explicit

{

    class Program

    {

        public interface aa

        {

            int sum(int a, int b);

            void display();

        }

 

        public interface bb

        {

            int subt(int a, int b);

            void display();

        }

 

        public class XX : aa, bb

        {

 

            #region aa Members

 

            int aa.sum(int a, int b)

            {

                return (a + b);

            }

 

            void aa.display()

            {

                Console.WriteLine("Display method of aa interface");

            }

 

            #endregion

 

            #region bb Members

 

            int bb.subt(int a, int b)

            {

                return (a - b);

            }

 

            void bb.display()

            {

                Console.WriteLine("Display method of bb interface");

            }

 

            #endregion

 

            public void display()

            {

                Console.WriteLine("This is display method of XX");

            }

        }

 

        static void Main(string[] args)

        {

            XX obj = new XX();

            // here I m calling explicitly methods of aa interface

            aa obj_a = new XX();

 

            obj_a.display();

            Console.WriteLine("Sum method of aa interface : " + obj_a.sum(2, 2));

 

            // here I m calling explicitly methods of bb interface

            bb obj_b = new XX();

            obj_b.display();

            Console.WriteLine("Subtract method of bb interface : " + obj_b.subt(12, 10));

 

            // here i m calling class XX display method

            obj.display();

 

            Console.ReadLine();

        }

    }

}

 

Something about implicit and explicit implementation of interfaces

 

You can also explicitly use interface methods.

 

You can bind methods with interface or class. When we use explicit implementation of interface method will bind with interface.

 

When we use implicit implementation methods are bind with class.

 

In explicit implementation you can't use any access modifier.

 

Interface support multiple inheritance

 

Although a class can only inherit from one other class, it can inherit from any number of interfaces. C# support multiple inheritance with help of interfaces.

 

When inheriting from a class and one or more interfaces, the base class should be provided first in the inheritance list followed by any interfaces to be implemented. For example

 

class MyClass : Class1, Interface1, Interface2, Interface3 { ... }

 

Practical demonstration of interface and class implementation

 

using System;

 

namespace interface_3

{

    class Program

    {

        public interface aa

        {

            int sum(int a, int b);

            void display();

        }

 

        public interface bb

        {

            int subt(int a, int b);

 

        }

 

        public class XX

        {

            public void show()

            {

                Console.WriteLine("I m in class XX");

            }

        }

 

        public class YY : XX, aa, bb

        {

            public int sum(int a, int b)

            {

                return (a + b);

            }

 

            public int subt(int a, int b)

            {

                return (a - b);

            }

 

            public void display()

            {

                Console.WriteLine("I m in class  YY");

            }

        }

 

        static void Main(string[] args)

        {

            YY obj = new YY();

            Console.Write("This method is of interface aa which is implemented by class YY");

            obj.display();

            Console.WriteLine("\n");

            Console.Write("This method is of class XX which is implemented by class YY");

            obj.show();

            Console.WriteLine("\n");

            Console.WriteLine("Method sum method of interface aa " + obj.sum(12, 12));

            Console.WriteLine("\n");

            Console.WriteLine("Method subt method of interface bb " + obj.subt(12, 12));

            Console.ReadLine();

        }

    }

}

 

Note: You can use shadowing (new) in interface also.

 

I think now you might be clear what interfaces are? How to implement interfaces and use it in a class? Have taken some definition from some references for technically defining interfaces.