Skip to main content

Interface in C#:-

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

        }
    }

                                  

Comments

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...