Interface in C#:-

0
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();
        }
    }
}


Example of Interface using Car and Sales?

interface CarDesign
    {
        void CarExteriorDesign();
        void CarInteriorDesign();
        void CarSecurity();
    }

    interface SalesRules
    {
        void SalesPolicy();
    }

    class Car : CarDesign,SalesRules
    {
       public void CarExteriorDesign()
        {
            Console.WriteLine("Exterior Design");
        }
       public void CarInteriorDesign()
        {
            Console.WriteLine("Interior Design");
        }
       public void CarSecurity()
       {
           Console.WriteLine("Security");
       }
      public void SalesPolicy()
       {
           Console.WriteLine("Sales Policy");
       }
       static void Main()
       {
           Car obj = new Car();
           obj.CarInteriorDesign();
           obj.CarExteriorDesign();
           obj.CarSecurity();
           Console.ReadKey();
       }
    }


Example of Interface in C#:-

interface I
    {
        void fun();   // body null
    }
    interface A
    {
        void fun();  //body null
    }

    class D
    {

    }

    class C : D,I,A
    {
        void I.fun()
        {
            Console.WriteLine("Fun Example I");
        }
        void A.fun()
        {
            Console.WriteLine("Fun Example A");
        }

        static void Main()
        {
            A obj = new C();
            obj.fun();
           I obj1 = new C();
            obj1.fun();
            Console.ReadKey();

        }
    }

                                  
Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)