Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Interface in C#:-


It is a special component of c# which will contain a set of abstract methods to writing program code which will be implemented into class.  The interface will be declared by the interface keyword.

The interface is mostly used to declare a set of rules, multiple inheritances, and data abstraction.

Interface decides how many features will be implemented into class for example in Car how many features should be defined that will be managed by Interface.

We can not create an object of interface hence interface will provide data hiding that is called data abstraction.

We can implement two different interfaces in a Single class that is called Multiple Inheritance


note:-  all method of interface is public and abstract by default in c#

Syntax of Interface:-

interface Intefacename
{
       void methodname1();
       void methodname2();
       void methodname3();
}

class Classname:Interfacename
{
      public void methodname1()
     {

     }
      public void methodname2()
     {

     }
      public void methodname3()
     {

     }
}

Example of Interface:-

interface Area
    {
        void rect();
        void triangle();
     
    }
    interface MathMatical
    {
        void addition();
        void substraction();
    }
    class AreaLogic : Area, MathMatical
    {
        public void rect()
        {
            Console.WriteLine("Rect");
        }
        public void triangle()
        {
            Console.WriteLine("Triangle");
        }
        public void addition()
        {
            Console.WriteLine("Addition");
        }
        public void substraction()
        {
            Console.WriteLine("Sub");
        }
        static void Main()
        {
            Area obj = new AreaLogic();
            obj.rect();
            obj.triangle();
            MathMatical obj1 = new AreaLogic();
            obj1.addition();
            obj1.substraction();
            Console.ReadKey();
        }
    }
}


                                  

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post