r/programminghelp • • Jan 06 '22

C# what is an interface c#

I have tried YouTube Reddit and discord with friends can anybody give like a hey stupid explanation of interfaces. What they are used for. What's the point of them, I just am not getting it.

3 Upvotes

2 comments sorted by

7

u/[deleted] Jan 06 '22 edited Jan 06 '22

Edit: just to say, I'm a java programmer, but the concepts are the same.

Think of a car (automatic or manual), they generally have the same interface:
Steering wheel, gear stick, foot pedals.
There are all sorts of cars, buses, trucks, different makes, different models, with different types of wheels, engines, etc... But generally speaking, if you can drive one car you can drive them all - because they all use the same interface.
Interfaces in programming are the same.

An interface contains a list of standard methods that are required in order for a class to be of that type. For a random example, say we're part of a team building a game, or even different games. And the boss says I want you both to create some game characters logic, you can do whatever you want, but you both must implement the "Character" interface. Let's say that interface contains the abstract methods moveLeft(), moveRight(), jump() and duck().

I make a character class called Cat. You make a character class called Dog. But because they both implement the Character interface whoever is adding them to the game doesn't have to care that it's a cat or dog, all they need to know is that it implements Character, and so has the methods that they need.

Then any game that is aware of the Character interface can use our Cat and Dog Characters, you can just drop them in and they'll work.

This provides another advantage in that you can store both cat and dog in a list of Characters, because they both implement the Character interface.

Consider the opposite, we don't have a Character interface, the boss just tells me to make a Cat Character and you to make a Dog Character. He tells us it's a side scroller and they need to be able Togo left, right, jump and duck.
I make mine with the methods goLeft() goRight(), jump() and duck(), but you make yours with moveLeft(), moveRight(), jump() and duck().
Now our characters are not compatible with each other. Turns out the guy making the main game was expecting the methods to be left(), right(), etc.....

Now we have problems. Also, there's not guarantee of reusability in the future either. 🙄

Another good example is a printer interface. It must have a method called print(). So your computer doesn't care what make or model the printer is, how the internals work, as long as the driver implements the printer interface you know it has the print() method.

Does that help? 🙂

1

u/ConstructedNewt MOD Jan 06 '22

An interface is a contract that a concrete piece of code must do some specific thing. It is a really powerful tool, as it enables abstraction while it states a clear intent of the code. And enables reusability and It gives compile time safety.

That was a lot of big words, let's look at an example:

obj A {
   B b = ... // an instance of B
}

This is an A that can now access its instance of B If B accesses a database then obj A, calling obj B will always only be able to do anything B can do. Furthermore for some situations you wouldn't even be able to use B, because you may not have the infrastructure needed or whatnot. A is strongly linked to B.

Now with an interface

obj A {
  Ib b = ... // this is an interface,  might be B
}

Now, A is strongly linked to the interface, but the interface only say that the instance at the location should fulfill its contract; given a method SomeMethod(SomeVariable variable, ...) returns SomeReturn this can be a lot of things. And A doesn't and shouldn't care what happens inside the instance (if that's important maybe they should be the same class, or both are already very specialised)

Let's say you have a piece of code that you can use to query and then delete files, it has a dry-run functionality, which only prints the results (no delete). The very simplified control flow could look like.

main() {
    ... //Create a querier
    List[Results] results = ...
    ResultHandler rh
    if dryRun {
        rh = ToStdOutResultHandler()
    } else {
        rh = DeletingResultHandler()
    }
    rh.handle(results)
}