Home > DeveloperSection > Blogs > Interface

Interface


C# C# 
Ratings:
0 Comment(s)
 1811  View(s)
Rate this:

Interface

An interface defines the syntactical contract that all the derived class should follow. Especially the interface defines what part of syntactical contract, and the classes implementing the interface describe the how part of the contract.

Interfaces also define properties, methods, and evens, which are known as members of the interfaces. It is important to note that interfaces contain only the declaration of the members. Classes implement these interface members. Interfaces are used when you want a standard structure of methods to be followed by the classes.

Declaring Interfaces

You can declare interface using the interface keyword.

public interface IOrderDetails

{

                //Declare an interface member

                void UpdateCustomerStatus();

                void TakeOrder();

}

Example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Interface

{

publicinterface IOrderDetails

{

//Declare an interface member

void UpdateCustStatus();

void TakeOrder();

}

publicclass ItemDetails : IOrderDetails

// by inheriting the interface, derived class must be implement all the methods declared in the interface,

// if we'll no do so, code will generate an error

{

publicvoid UpdateCustStatus() //implementing the Interface Method

{

Console.WriteLine("Customer Name is : Jacob ");

}

publicvoid TakeOrder() //implementing the Interface Method

{

Console.WriteLine("Order Placed ");

}

publicvoid BillAmount()

{

Console.WriteLine("Bill Amount is : 1000");

}

}

classProgram

{

staticvoid Main(string[] args)

{

ItemDetails objItemDetails = new ItemDetails();

objItemDetails.UpdateCustStatus();

objItemDetails.TakeOrder();

objItemDetails.BillAmount();

}

}

}

Output:

Customer Name is: Jacob

Order Placed

Bill Amount is: 1000


Don't want to miss updates? Please click the below button!

Follow MindStick