Anda di halaman 1dari 6

1

Irina Scurtu - Interfaces

Interfaces

Polymorphism is not achieved only via inheritance but also via interfaces.

Unlike abstract classes interface can’t have any implementation. Like abstract classes, interfaces
define a set of members that callers need to implement.

By implementing an interface, a type(class) defines its capabilities. The interface defines a contract that
tells a class that it needs to provide an implementation, to be compliant with the interface.

It’s like telling to a class :” ok, I oblige you to give an implementation for these members”

The classes must declare and implement methods with the same signatures as the methods
declared in interface.

Interface declaration
interface ILandBound
{
void Run();
int GetSpeed();
}

In this example, the classes that use this interface must supply an implementation for the Run() method
that returns a void, and for the GetSpeed() method that returns an integer.

Think about power plugs and the sockets in the wall. All the power plugs that have a right
interface will fit in the socket. It’s the same with interfaces.

In an interface you can define methods, properties and events, but no fields, because the fields
hold state, and that state is specific to a class and those are part of the implementation details.
(remember that we must keep things encapsulated).

A member declared in an interface can’t have an access modifier, these are assumed to be
public by default. The reason behind this is that access modifiers prevent external code to access that
member.
2
Irina Scurtu - Interfaces

Implementing an interface
Declaring that a class implements an interface is similar to inheriting from a base class.
public class Horse : ILandBound
{
public int GetSpeed()
{
var speed = 20;
return speed;
}

public void Run()


{
Console.WriteLine("Horse, Run");
}
}

A class can implement multiple interfaces but inherit only from a single base class.
public class Horse : Mammal, ILandBound, ISoundProducible
{
public int GetSpeed()
{
var speed = 20;
return speed;
}

public void MakeSounds()


{
throw new NotImplementedException();
}

public void Run()


{
Console.WriteLine("Horse, Run");
}
}

Once a class declares that it implements an interface, it must implement all the members
specified in the ‘contract’

One important characteristic of interface is that it can’t be instantiated (same like abstract classes), and
don’t have constructors or finalizers, or have static members.
3
Irina Scurtu - Interfaces

In code, an interface is accessible only through the types that implement it, not directly, but you can
declare variables of an interface type. Doing this you will restrict the type assigned to that variable only
to what is specified in the interface.

ILandBound landBoundMammal = new Horse();


landBoundMammal.Run();

landBoundMammal.GetSpeed();

By using this kind of code, even though you instantiate a Horse on the landBoundMammal variable, you
will have access only to the members defined in the interface( methods Run() and GetSpeed()) and
nothing from the base type Mammal, or from the ISoundProducible interface.

By using interfaces you can have for example a variety of types in the same array, if all share a common
interface.

Implicit implementation

Interface inheritance
Since in C# there is no way to inherit from multiple classes, there is a way to ‘mimic’ multiple
inheritance.

For example
interface IHuman
{
void Eats();
void Sleeps();
}

interface ICitizen
{
bool Vote();
}

interface IStudent: ICitizen, IHuman


{
void AttendsClasses();
}
4
Irina Scurtu - Interfaces

If we implement the interface IStudent in a class, we will need to implement all the members in all
interfaces that compose IStudent.
public class Student : IStudent
{
public void AttendsClasses()
{
throw new NotImplementedException();
}

public void Eats()


{
throw new NotImplementedException();
}

public void Sleeps()


{
throw new NotImplementedException();
}

public bool Vote()


{
throw new NotImplementedException();
}
}

This kind of implementation is called implicit.

There is another kind of implementation, called explicit implementation that usually solves name
clashes.

Let’s say that in two interface we have the same members. In this case we would need to find a way to
solve this name clash.
5
Irina Scurtu - Interfaces

interface ITaxCalculator
{
double Add();
double Subtract();
}

interface ICalculator
{
double Add();
double Subtract();
double Multiply();
double Divide();
}

public class Calculator : ICalculator, ITaxCalculator


{
public double Add()
{
throw new NotImplementedException();
}

public double Divide()


{
throw new NotImplementedException();
}

public double Multiply()


{
throw new NotImplementedException();
}

public double Subtract()


{
throw new NotImplementedException();
}

In Calculator class, there is no way to distinguish which interface the methods Add() and Substract()
belong to, so we need to explicitly implement the interfaces.
6
Irina Scurtu - Interfaces

public class Calculator : ICalculator, ITaxCalculator


{
public double Add()
{
throw new NotImplementedException();
}

public double Subtract()


{
throw new NotImplementedException();
}

double ICalculator.Add()
{
throw new NotImplementedException();
}

double ICalculator.Divide()
{
throw new NotImplementedException();
}

double ICalculator.Multiply()
{
throw new NotImplementedException();
}

double ICalculator.Subtract()
{
throw new NotImplementedException();
}
}

Basically , to explicitly implement an interface means to prefix the member with the interface name, to
help the compiler understand (and you) which is the interface you are referring to with that
implementation.