Interfaces
There are two types inheritance: Class inheritance which has been allready described and Interface inheritance (subtyping).
An interface looks like a concrete class with one important difference: properties and methods are only declared but not defined. An object can not be created from type of an interface!
- interface IAccount // Definition of an interface
- {
- int initialBalance { get; } // no definitions of methods and properties - only deklarations
- int transaction(double amount); // all methods are public by default
- double getBalance();
- }
Example of an interface in C#
Subsequently interfaces specify only what the class is doing, not how it is doing it. The implementing class of that interface has to define every method wich is declared by the interface and specifiy how to do it:
- class Account : IAccount // Interface is implemented by class
- {
- protected double balance;
- public Account()
- {
- balance = INITIAL_BALANCE;
- }
- int INITIAL_BALANCE
- { // Methods declared by implemented interfaces
- get // now have to be defined
- {
- return 50;
- }
- }
- public int transaction(double amount)
- {
- balance += amount;
- return 0;
- }
- public double getBalance()
- {
- return balance;
- }
- }
Example of an class which implements this interface in C#
An interface defines what the behavior a an object will have, but it will not actually specify the behavior. It is a contract, that will guarantee, that a certain class can do something.

UML class diagram: Implementation of an interface
Summary
- An object of an interface cannot be created,
IAccount iAccount = new IAccount();
- but an interface describes what the implementing class should can do, without specifying how to do it.
- Subsequently a class that implements interface must implements all the methods and properties in interface.
- A class can implement more than one interface. That means it can be used used to achieve multiple inheritance.
- An interface can extends one or more another interfaces.
- C# do not allow fields in interfaces.