Per Erik Strandberg /cv /kurser /blog

I want to test what happens if a class inherits two or more interfaces with the same method.

I make a silly example of a class that implements Spin in two different ways. First a ballerina-interface where Spin would mean that the ballerina rotates at a certain speed. Then a cat-interface. The Swedish word for purring is Spin, so do not be confused if it makes no sense in your favourite language.

This example is available here: [1]

## Inheritance Diagram

It is this simple...

``` Object
|
+--- IBallerina
|
+--- ICat
|
myDancingKitten
```

## IBallerina

Any ballerina can spin and answer if they need any toes cut to fit into a shoe of a certain size.

```  // a dancer in a silly dress
public interface IBallerina
{
void Spin(int RPM);

bool NeedToCutToe(int ShoeSize);
}
```

## ICat

Cats can spin (purr in english) and answer if they need a shave.

```  // Felis catus
public interface ICat
{
bool NeedsShave();

void Spin(int RPM);
}
```

## The class myDancingKitten

First we need a to implement the spin-, needsshave- and needtocuttoe-methods and the other functions in a small class.

```  // A Felis catus in a silly dress
public class myDancingKitten : IBallerina, ICat
{
public void Spin(int RPM)
{
Console.WriteLine("purr screeeeeeeech!!!!");
}

public bool NeedsShave()
{
//
// shaving cats is fun, right?
// [2]
//
return true;
}

public bool NeedToCutToe(int ShoeSize)
{
// all cats have very small feet
return false;
}
}
```

## Small test class

We make a small test class with the original name program. Here we create three functions that take a ballerina, a cat and a dancing kitten and then calls its Spin function.

```  static class Program
{
static void PlayWithBallerina(IBallerina balle)
{
balle.Spin(216);
}

static void PlayWithCat(ICat kitty)
{
kitty.Spin(666);
}

// if I ever get a kitten that can danse I'll call it Belzeebub
static void PlayWithMyDancingKitten(myDancingKitten Belzeebub)
{
Belzeebub.Spin(314);
}

static void Main(string[] args)
{
myDancingKitten Belzeebub = new myDancingKitten();

PlayWithBallerina(Belzeebub);
PlayWithCat(Belzeebub);
PlayWithMyDancingKitten(Belzeebub);

Console.Write("Press Andy to continue . . . .");
}
}
```

## First Output

```purr screeeeeeeech!!!!
purr screeeeeeeech!!!!
purr screeeeeeeech!!!!
Press Andy to continue . . . .
```

Since it is obvious that we intend different things when we spin the cat as if it was a cat or it was a ballerina (and perhaps also as if it was a dancing kitten).

We add two more cases of the spin-method in the dancing kitten class:

```    void IBallerina.Spin(int RPM)
{
Console.WriteLine("screeeeeeeech!!!!");
}

void ICat.Spin(int RPM)
{
Console.WriteLine("purr purr...");
}
```

We now have three Spin-methods.

1. First one method that tells us how the dancing kitten will spin if it is expected to behave as a ballerina. Clearly the poor cat does not like being tossed around like that.
2. Also a method that tells us how the cat behaves if he is purring. This is nice. The cat likes to purr.
3. A final one (the first one we implemented) that tells us how the cat behaves if someone knows he is a dancing kitten and wants him to spin. This is a little strange for the cat - first he likes it then he throws up.

The output is different now. The testprogram is the same, but the class is different.

```screeeeeeeech!!!!
purr purr...
purr screeeeeeeech!!!!
Press Andy to continue . . . .
```

## Conclusion

This is really nice clearly a dancing kitten must be able to behave both as a cat and as a ballerina (even if he does not like it). Also a dancing kitten can behave just as a dancing kitten. This allows for nice and flexible inheritance, but still with some sort of strictness.