Learn OOP Interface Using C#

Before reading this article, please go through the following articles:

  1. Object Oriented Programming Using C#: Part 1
  2. Object Oriented Programming Using C#: Part 2
  3. Object Oriented Programming Using C#: Part 3
  4. Object Oriented Programming Using C#: Part 4
  5. Object Oriented Programming Using C#: Part 5
  6. Object Oriented Programming Using C#: Part 6
  7. Object Oriented Programming Using C#: Part 7
  8. Object Oriented Programming Using C#: Part 8
  9. Object Oriented Programming Using C#: Part 9
  10. Object Oriented Programming Using C#: Part 10

Interface

Dear reader, today we will discuss another important component of OOP, interfaces. This topic again confuses OOP bginners. I have even found many questions in various forums regarding interfaces.

  • What is an interface?
  • When to use an interface?
  • Why use an interface?
  • Where to use an interface?

So I will explain interfaces with an example. When you press the "power" button of your machine, it's an interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the machine on and off.

img1.jpgimg2.jpg

In simple words, an interface defines how you can interact with a class, in other words what methods it supports.

What is an interface?

An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class).

When to use an interface?

This is a very important question for the new developer. For example we have a core banking system. We all know that bank data is very sensitive. A little negligence is risky. So if the bank decided to develop a mobile banking web application from a third party developer then we can't provide full access to our main core banking application. So we must design the interface in our core banking application. You might use a DLL as well.

The other developers can use that DLL and send data for the transition in the main application. The third-party developers only access the core banking application with the limited rights that we have given them in the interface. So the interface is very useful in such conditions.

Simple Example

The following is the simple example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Interfacelesson
{
    class Program
    {  
        interface ISupercar
        {
            /// Interface method declaration
            bool smartcar();
        }
         class mercedes :  ISupercar
        {
            public mercedes()
            {            
            }
            /// Interface method definition in the class that implements it
            public bool smartcar()
            {
                Console.WriteLine("you have smart car");
                return true;
            }       
        }
        static void Main(string[] args)
        {
            ISupercar mycar = new mercedes();
            mycar.smartcar();
            Console.Read();
        }
    }
}

Output

Interface Using C#

Block A

Block A is:

  • In this sessoion we have defined the interface ISupercar.
  • Has one method, smartcar, without implementation.

Block B

Block B is:

  • In this sessoion we have defined the class Mercedes with the implemented interface ISupercar.
  • Having one method smartcar with implementation.

Block C

Block C is:

  • Create one object of the interface using "ISupercar mycar = new mercedes();".
  • Using that object "mycar" we access the method that we have defined in the class mercedes.

Why use an interface?

  • Create loosely coupled software
  • Support design by contract (an implementer must provide the entire interface)
  • Allow for pluggable software
  • Allow objects to interact easily
  • Hide implementation details of classes from each other
  • Facilitate reuse of software

Where to use an interface?

C# does not support multiple inheritances. An interface on the other hand resolves the multiple inheritances problem. One class can implement many interfaces.

An Advance Example

In this example we will create the two small projects. In the first project we will create a DLL file and define one interface. In the second project we will call the DLL file that we have designed in the first project, then we will use the interface of the first project. This will help us to understand the concept of interfces more clearly.

In this project our concept is very simple. We will create the core banking application concept and then we will create the DLL of the core banking project and use it in the mobilebanking project.

Step 1

Create the new project with the name of "learninterface" with project type Class Library.

Interface Using C#

Step 2

We will rename Class1 to Corebanking.

Interface Using C#

Step 3

Now we will define the interface with the name of "Icorebanking" with the one method "void updatetransition(int a, int b);" without implementation because an interface does not allow implementation of the method in the interface and it's a good practice to always use the capital "I" for the interface name.

Step 4

Now we will implement the interface in a class Corebanking. So we must implement all the methods of the interface in the class Corebanking.

public interface Icorebanking
{
    void updatetransition(string cusId, string Ref, int amtdeposit, int amtcredit);
}
public class Corebanking : Icorebanking
{
    public void updatetransition(string cusId, string Ref, int amtdeposit, int amtcredit)
    {
        Transition trans = new Transition();
        trans.save(cusId, Ref, amtdeposit, amtcredit);
   }
}

Step 5

Add the new class with the name "Main".

Interface Using C#

Step 6

Now we will create the function GetDBConnection as a public function so it can be accessed from anywhere in the project. But you must modify the data source setting according to your own settings.

For Example

  1. SQL Server instance name. (.\\CRMIS)
  2. SQL Server user name and password. (user id sa password.###Reno123)
  3. Initial Catalog=PSH (database name)

Add the public static class in the Main.cs:

public static SqlConnection GetDBConnection()
{
    SqlConnection conn = new SqlConnection(
    "Data Source=.\\CRMIS;Initial Catalog=PSH;User ID=sa;Password=###Reno321");
    return conn;
}

Step 7

CREATE TABLE [dbo].[Transition](
[TRA_Customer_Id] [varchar](50) NULL,
[TRA_Account_Ref] [varchar](15) NULL,
[TRA_Amount_Deposit] [money] NULL,
[TRA_Amount_Credit] [money] NULL
) ON [PRIMARY]

Step 8

Add the new class with the name "Transition" and insert the following method into it.

Interface Using C#

Step 9

Now add the method into the Transition class.

public void save(string cusId,string Ref,int amtdeposit,int amtcredit)
{
    string SQL = @" insert into psh.dbo.Transition
    (TRA_Customer_Id,TRA_Account_Ref,TRA_Amount_Deposit,TRA_Amount_Credit)
    values( '" + cusId + "'      ,'"+ Ref +"','" + amtdeposit + "','" + amtcredit + "')";
    SqlConnection con = Main.GetDBConnection();
    DataTable consultanttable = new DataTable();
    SqlDataAdapter Consultantdataadapter = new SqlDataAdapter(SQL, con);
    Consultantdataadapter.Fill(consultanttable);
}

Step 10

Now add another project as shown in the diagram with the name of MobileBanking:

Interface Using C#

Interface Using C#

Step 11

Interface Using C#

Interface Using C#

It's an important step. First we must add the referance of the DLL file to the MobileBanking project, then we will design the form1.

Step 12 

Interface Using C#

Set the project startup type.

Step 13 

Interface Using C#

Now we need to design the form as shown in the picture to send data into the datecase using the interface DLL created in the first project.

Step 14

Interface Using C#

In that step we will insert the following code into the button.

Step 15

Interface Using C#

Now execute the project and enter the following data into the textboxes and click the Save button.

Step 16

Interface Using C#

Now check the database and you will see the following result.


Recommended Free Ebook
Similar Articles