C
C#β€’3y ago
Ice_

❔ When to use an Interface? Example?

I'm not entirely sure what the difference between interface vs abstract class is. As far as I know interfaces has no properties - just methods? Got any good examples on that? I've tried googling a bit but I'm finding variating results!
12 Replies
sibber
sibberβ€’3y ago
interfaces can have properties they shouldnt have any implementations use interfaces to define the public api abstract classes can have non abstract methods, private variables..
Ice_
Ice_OPβ€’3y ago
That doesn't make it any clearer for me :/ They are just a "mold" of something? Got any good examples? Thank you for providing the information though! I'm not that familiar with abstract classes nor interfaces. I'm here to learn so that's why I'm asking. As I've said I've tried googling but the the answers are a bit confusing for me so that's why I came here πŸ˜…
cray.io
cray.ioβ€’3y ago
Think of an interface as like a type of contract. When a class extends an interface, it has to full that contract, which means implementing the properties etc defined within it. This is useful if you want to, for example, create different types of classes that can be acted upon in the same manner. Say you have a method Write, and you wanted to be able to pass to this method different types of mediums to write to - a stream, a file, a socket, the console. You can define a common interface that they will all abide by. let’s say that we have an interface IWriter which defines a method called WriteString, which actually implements the writing mechanism. If we define the Write method to accept an IWriter type as an argument, you can pass any implementation(class) of that interface, and as long as that implementation has defined WriteString, Write should be able to call it. So we could have a class called ConsoleWriter, StreamWriter, SocketWriter, and if they all extend and properly implement the WriteString method, Write can accept an instance of those classes and act upon them accordingly
cnetworks
cnetworksβ€’3y ago
hi thank you.where abstract class differes from here? can you please.
sibber
sibberβ€’3y ago
an abstract class can be used for this, but an interface makes for sense because it was made for this purpose also you can implement multiple interfaces but you cant inherit from multiple classes structs can also implement interfaces
D.Mentia
D.Mentiaβ€’3y ago
That multiple inheritance is super important and basically the reason interfaces even exist; so if you're asking why use an interface, the answer is basically multi-inheritance - a class can inherit multiple interfaces C# specifically chose to not allow you to inherit multiple classes because doing so causes the "diamond problem" where it's ambiguous which method your class should call: Class B : A Class C : A Class D : B,C If A, B and C are all classes that define DoThing, when you call D.DoThing(), which thing is it doing? Interfaces get around this by making sure that if you inherit multiple things, they can't have any definitions (so you'd have to define D.DoThing). By guaranteeing there's no implementation, you can multi-inherit safely
sibber
sibberβ€’3y ago
except that interfaces can have default implementations now lol but they shouldnt imo
D.Mentia
D.Mentiaβ€’3y ago
lol right, but any non-default implementations overrides them (and I like to pretend they don't exist) Though I guess ... did they... re-introduce the diamond problem by letting interfaces have default implementations? As above, but pretend A,B,C are all interfaces that define DoThing
cnetworks
cnetworksβ€’3y ago
By guaranteeing there is no implementation,uou can multi-inherit safely, Could you pls elaborate on that?
D.Mentia
D.Mentiaβ€’3y ago
If you have 30 interfaces that all define DoThing(), you can inherit them all just fine, because none of them actually defined an implementation, so it never has to guess which one to run. You'll define one in your class, and it will run that one. If you had 30 classes that all defined DoThing() and you were allowed to inherit more than one, it wouldn't know which DoThing to call
cnetworks
cnetworksβ€’3y ago
wonderful explanation.Thanks
Accord
Accordβ€’3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server