Osman Khalid, Lecturer, Computer Sciences Department, COMSATS Institute of Information Technology

Lecture (16 Oct)
Interfaces

Declaring an interface works syntactically in pretty much the same way as declaring an abstract class. You should be aware, however, that it is not permitted to supply implementations of any of the members of an interface. In general, an interface can only contain declarations of methods, properties, indexers, and events.
You can never instantiate an interface; it contains only the signatures of its members. An interface has neither constructors (how can you construct something that you can ’ t instantiate?) nor fields (because that would imply some internal implementation). To begin, you need to define the IBankAccount interface:
namespace Wrox.ProCSharp { public interface IBankAccount { void PayIn(decimal amount); bool Withdraw(decimal amount); decimal Balance { get; } } }

Let ’ s start off with the first class, a saver account run by the Royal Bank of Venus:
namespace Wrox.ProCSharp.VenusBank { public class SaverAccount : IBankAccount { private decimal balance; public void PayIn(decimal amount) { balance += amount; } public bool Withdraw(decimal amount) { if (balance > = amount) { balance -= amount; return true; } Console.WriteLine(“Withdrawal attempt failed.”); return false; } public decimal Balance { get { return balance; } } public override string ToString() { return String.Format(“Venus Bank Saver: Balance = {0,6:C}”, balance); } } }

To illustrate how different classes can implement the same interface, assume that the Planetary Bank of

Downloaded from: www.onspot.pk

} } } This code (which if you download the sample.Osman Khalid.Withdraw(600).Withdraw(100). jupiterAccount. that we’d get a compiler error if we tried something like this: accounts[1] = new SomeOtherClass(). // SomeOtherClass does NOT implement // IBankAccount: WRONG!! Derived Interfaces It ’ s possible for interfaces to inherit from each other in the same way that classes do. you can test them out.WriteLine(jupiterAccount. Lecturer. Jupiter Bank Saver: Balance = £ 400.JupiterBank { public class GoldAccount : IBankAccount { // etc } } We won ’ t present details of the GoldAccount class here. decimal amount).ProCSharp { public interface ITransferBankAccount : IBankAccount { bool TransferTo(IBankAccount destination. using Wrox.ProCSharp.ProCSharp { class MainEntryPoint { static void Main() { IBankAccount venusAccount = new SaverAccount(). it ’ s basically identical to the implementation of SaverAccount . using Wrox.JupiterBank.pk . where each element of the array is a different class: IBankAccount[] accounts = new IBankAccount[2].ProCSharp.ToString()).00 reference is that it can refer to any class that implements that interface. For example. } } Downloaded from: www. We stress that GoldAccount has no connection with SaverAccount .ToString()).PayIn(500).onspot. however. Note. Computer Sciences Department. this allows you to form arrays of interfaces. other than that both happen to implement the same interface. IBankAccount jupiterAccount = new GoldAccount().ProCSharp. Now that you have your classes.cs ) produces this output: C: > BankAccounts Venus Bank Saver: Balance = £ 100.VenusBank. accounts[1] = new GoldAccount().00 Withdrawal attempt failed. This concept is illustrated by defining a new interface. ITransferBankAccount .PayIn(200). You first need a couple of using statements: using System. venusAccount. jupiterAccount. Now you need a Main() method: namespace Wrox. using Wrox.Withdraw(100). Console. venusAccount. in the sample code. which has the same features as IBankAccount but also defines a method to transfer money directly to a different account: namespace Wrox. COMSATS Institute of Information Technology Jupiter also implements a class to represent one of its bank accounts — a Gold Account: namespace Wrox. accounts[0] = new SaverAccount(). Console. jupiterAccount.ProCSharp. you can find in the file BankAccounts.WriteLine(venusAccount.

”).pk . ITransferBankAccount jupiterAccount = new CurrentAccount(). 100). } } public bool TransferTo(IBankAccount destination.WriteLine(“Withdrawal attempt failed. if ((result == Withdraw(amount)) destination. } public bool Withdraw(decimal amount) { if (balance > = amount) { balance -= amount.PayIn(amount). Computer Sciences Department.Format(“Jupiter Bank Current Account: Balance = {0. Console.TransferTo(venusAccount.ToString()).cs ) produces the following output. } Console.PayIn(500). as you can verify. public void PayIn(decimal amount) { balance += amount. Console.ToString()).00 Jupiter Bank Current Account: Balance = £ 400. } } The class can be demonstrated with this code: static void Main() { IBankAccount venusAccount = new SaverAccount().PayIn(200). return false. jupiterAccount.onspot. Lecturer.WriteLine(venusAccount. return true. balance).00 Downloaded from: www.WriteLine(jupiterAccount. jupiterAccount. shows that the correct amounts have been transferred: C: > CurrentAccount Venus Bank Saver: Balance = £ 300. venusAccount. return result. which. decimal amount) { bool result. } public decimal Balance { get { return balance. COMSATS Institute of Information Technology public class CurrentAccount : ITransferBankAccount { private decimal balance.Osman Khalid. } This code ( CurrentAccount.6:C}”. } public override string ToString() { return String.