Interfaces in C#

Posted on Updated on

Image result for Interfaces in C#Interfaces in C#

In C#, interfaces are a way to attain run-time polymorphism. It is just like a class but has no implementation. An interface can contain methods, properties, methods or indexers. It is a syntactical contract that all class that all the classes that inherit the interface must follow. The interface defines the ‘what’ part while the deriving the classes define the ‘how’ part of the syntatical contract. interfaces have only the declarations and it is the responsibility of the deriving class to define the members. It provides the standard structure that the deriving class must follow.
As C# doesnot support multiple inheritance, interfaces provide the way to include the methods/ properties from multiple sources. The interfaces can also be used in structs if want to achieve inheritance for the structs as they can’t inherit from other class or structs.
A class can inherit multiple interfaces and even an interface can inherit multiple interfaces. A class or struct that is inheriting the interface must implement all the members of the interface. to implement the member of the interface the member of the implementing class must have the same name and signature as the interface member and the corresponding member of the implementing class must be public and non-static.
If a super class implements an interface, all the sub classes of the super class inherits that implmentation.

Properties of the interfaces:
=============================
1) An interface is just like the abstract base class and the class or struct implementing it must implement all its members.
2) It is not possible to create the instance of the class.
3) Interfaces can have events, indexers, properties and methods.
4) No method in the interface has definition.
A class or struct may have implementation of any number of interfaces and a class can inherit the base class and also other interfaces.

Declaring interfaces

Interfaces are declared using interface keyword. Statements of interface are by default public.

public interface ITransactions
{
// interface members
void showTransaction();
double getAmount();
}

Example

The following example demonstrates implementation of the above interface:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;

namespace InterfaceApplication
{
public interface ITransactions
{
// interface members
void showTransaction();
double getAmount();
}

public class Transaction : ITransactions
{
private string tCode;
private string date;
private double amount;
public Transaction()
{
tCode = ” “;
date = ” “;
amount = 0.0;
}

public Transaction(string c, string d, double a)
{
tCode = c;
date = d;
amount = a;
}

public double getAmount()
{
return amount;
}

public void showTransaction()
{
Console.WriteLine(“Transaction: {0}”, tCode);
Console.WriteLine(“Date: {0}”, date);
Console.WriteLine(“Amount: {0}”, getAmount());
}
}
class Tester
{
static void Main(string[] args)
{
Transaction t1 = new Transaction(“001”, “8/10/2012”, 78900.00);
Transaction t2 = new Transaction(“002”, “9/10/2012”, 451900.00);
t1.showTransaction();
t2.showTransaction();
Console.ReadKey();
}
}

OUTPUT:

When the above code is compiled and executed, it produces the following result:

Transaction: 001
Date: 8/10/2012
Amount: 78900
Transaction: 002
Date: 9/10/2012
Amount: 451900

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s