Learn OOP Interface Using C#
Here you learn about Interface an OOP Component using c#.
Before reading this article, please go
through the following articles:
- Object Oriented Programming Using C#: Part 1
- Object Oriented Programming Using C#: Part 2
- Object Oriented Programming Using C#: Part 3
- Object Oriented Programming Using C#: Part 4
- Object Oriented Programming Using C#: Part 5
- Object Oriented Programming Using C#: Part 6
- Object Oriented Programming Using C#: Part 7
- Object Oriented Programming Using C#: Part 8
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.
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.
The following is the simple example:
/// Interface method declaration
class mercedes : ISupercar
/// Interface method definition in the class that implements it
public bool smartcar()
Console.WriteLine("you have smart car");
static void Main(string args)
ISupercar mycar = new mercedes();
Block A is:
- In this sessoion we have defined the interface ISupercar.
- Has one method, smartcar, without implementation.
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 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.
Create the new project with the name of "learninterface" with project type Class Library.
We will rename Class1 to Corebanking.
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.
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);
Add the new class with the name "Main".
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.
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");
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]
Add the new class with the name "Transition" and insert the following method into it.
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
values( '" + cusId + "' ,'"+ Ref +"','" + amtdeposit + "','" + amtcredit + "')";
SqlConnection con = Main.GetDBConnection();
DataTable consultanttable = new DataTable();
SqlDataAdapter Consultantdataadapter = new SqlDataAdapter(SQL, con);
Now add another project as shown in the diagram with the name of MobileBanking:
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.
Set the project startup type.
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.
In that step we will insert the following code into the button.
Now execute the project and enter the following data into the textboxes and click the Save button.
Now check the database and you will see the following result.