Shortcuts

Why is there a need for an interface?

I'm still relatively inexperienced as a C# developer. One concept that I haven't yet grasped is that concerning 'interfaces'. I understand that C# does not support multiple class inheritance, and interfaces are therefore sometimes used. However, I'm
still trying to understand why interfaces are needed. There's a good tutorial on C# with an example on interfaces at
http://www.tutorialspoint.com/csharp/csharp_interfaces.htm

In the example shown, the class Transactions inherits from the interface
ITransactions. Why is it even necessary to have this interface?

The Transactions class has methods named getAmount() and
showTransaction() just as the interface ITransactions
does. Accordingly, why can't these methods in the Transaction class simply be called from the Main method in the
Tester class and eliminate any need for the interface ITransactions
altogether?

So your real code will using a proper Transactions class that interacts with the database. Howeven when you're testing your Baking class via unit tests, you can mock an ITransactions class and use that with the Banking class;

Banking b = new Banking(mockedITransactionsObject);

b.TransferMoney ( ... );

that way you can test the logic in TransferMoney without needing an actual transaction manager that needs a database.

That's just one reason for using interfaces. You might also want an ITransactions class that does transactions for SQL Server and one for Oracle, or Access or whatever, and to use Banking against the right database you simply suppy the appropriate Transactions
class that talks to your chosen database.

I'm afraid I no longer use this forum due to the new point allocation system.

Re: Why is there a need for an interface?

In non-programming terms - lets say we have two banks and we wish to transfer some money betwen them. Before we can do so, we must agree on a mechanism that we will use. The bank that provides this mechanism - we shall call the service provider, the other
bank will be the service consumer - the agreement they form on how to use the service is called a contract of service - or just contract. You can query each bank to see if they provide the service you want. The collection of services implemented determines
what "type" of bank it is.

If this service were implemented electronically, they may choose to use an API (Application Programming Interface) - which is basically a set of functions that implement a known set of behaviour. Banks being large entities may implement many APIs for lots
of different things.

In C# terms, providing a contract is done by usnig an Interface - the set of methods that the class must implement to provide a piece of functionality. If a class implements multiple contracts (provides multiple services) it must implement multiple interfaces.

Generics (or Polymorphism) is the ability to refer to classes by the interfaces/contracts that they implement (in other words its Type).

We can take this further by allowing a class to implement an interface differently from another classes implementation. In this case we could have a TextReader interface that when implemented one way might read from a file on disk, we might have another
class that might read from the keyboard. In both cases the methods they implement are the same, but the action they perform is different.

Re: Why is there a need for an interface?

Toppeak

In the example shown, the class Transactions inherits from the interface
ITransactions. Why is it even necessary to have this interface?

The Transactions class has methods named getAmount() and
showTransaction() just as the interface ITransactions
does. Accordingly, why can't these methods in the Transaction class simply be called from the Main method in the
Tester class and eliminate any need for the interface ITransactions
altogether?

Here is how I would like to look into it.

The above example ITransactions is like a contract. It will represent any Transaction that may take place in your entire program. All Transactions will have a
showTransaction() and getAmount() but each one might modify it and use it differently.

So, what are interfaces good for if they don't implement functionality? They're great for putting together plug-n-play like architectures where components can be interchanged at will. Since all interchangeable components implement the same
interface, they can be used without any extra programming. The interface forces each component to expose specific public members that will be used in a certain way. Interface is useful in several ways here.

Firstly - It categorizes all Transactions in your program.

Secondly - You may specify the basic methods or standard that all transactions in your program will have and yet you may inherit and define them differently. It's like plug and play.
An interface contains only the signatures of
methods, properties,
events or
indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.