0
\$\begingroup\$

Interfaces are a feature of C# that I've never quite been able to see the purpose of. I see them used all the time in professional code, but for the life of me I can't work out the reasoning.

My question here is specifically about interfaces to simulate multiple inheritance. In reading around the subject, I'll often come up on an example like this:

interface IAddition
{
    int add(int a, int b);
}

interface ISubtraction
{
    int sub(int a, int b);
}

class Calculation : IAddition, ISubtraction
{        
    public int add(int a, int b)
    {
        return result1 = a + b;
    }

    public int sub(int a, int b)
    {
        return result1 = a - b;
    }
}

What I don't understand is - what benefit do the interfaces bring here? It looks to me like you could remove them completely, and the Calculation class would still work exactly the same. What am I missing here?

Any help would be appreciated - I've been looking for something to make interfaces 'click' in my head for quite a while now.

The reason I look for the solution to this is for making games. Multiple inheritance seems to be a good solution for making flexible game objects, but I've haven't quite got a proper grasp of it yet.

\$\endgroup\$
8
  • 3
    \$\begingroup\$ This question doesn't seem to be about game development. \$\endgroup\$ – Peter Nov 13 '16 at 12:46
  • \$\begingroup\$ I should say I'm a game developer, so I was thinking about it in terms of that. Its games' source code that I've seen interfaces so often used, and games where many examples can be found of the usefulness of multiple inheritance. C# is the language that Unity uses as well, so you can see my thinking at least. I guess I didn't consider this could work as a more general question. \$\endgroup\$ – Infergnome Nov 13 '16 at 13:24
  • 2
    \$\begingroup\$ gamedev.stackexchange.com/help/on-topic General programming questions are off topic on this site. There are 3 other sites on stackoverflow where these can be asked, one of them being stackoverflow.com. \$\endgroup\$ – Peter Nov 13 '16 at 17:43
  • \$\begingroup\$ For asking in context of game dev, you seem to make no mention of gaming. To me, this is a very clear strictly programming question. \$\endgroup\$ – Gnemlock Nov 13 '16 at 20:46
  • \$\begingroup\$ The one game-relevant detail I can share here is that reliance on single inheritance often proves problematic in games. When we start writing the code for a game, we're often a very long way from what the final game will be - we rarely get a clean architecture plan solidified and honoured throughout development. When designs change, the "is" assumptions of inheritance hierarchies can be broken very easily, compared to the "has-the-capabilities-of" expression we get from interfaces or components. For this reason, a lot of game code shies away from reliance on deep inheritance hierarchies. \$\endgroup\$ – DMGregory Nov 13 '16 at 20:57
2
\$\begingroup\$

An interface represents a contract, they're used to allow a method to state that it needs something that fulfills that contract, when the actual details of that contract aren't known.

You example is a bit weird because things aren't usually both an addition and a subtraction, it might make more sense for it to be something like this;

public interface ICalculation {
    int DoOperation(int a, int b);
}    

public class Addition : ICalculation {
    public int DoOperation(int a, int b) {
        return a + b;
    }
}

public class Subtraction : ICalculation {
    public int DoOperation(int a, int b) {
        return a - b;
    }
}

public class Calculator {
    int GetResult(ICalculation calculation, int a, int b) {
        return calculation.DoOperation(a, b);
    }
}

The Calculator does not need to know how to add now, only that it needs to use the Addition implementation of the ICalculator contract.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.