C
C#4w ago
Faker

✅ Interfaces in C#

Hello guys, I just learnt about interfaces, just to have an overview of what it is. From what I've understood, an interface is just a sort of "contract". That is, we say that in this particular interface, we want to have method X and property Y. Any class implementing that interface must also have method X and property Y but the way how we define the method body can be different. I also read that "multi-inheritance" is not allowed I think in C# (just like in java I think). That's why, interfaces can be helpful because we can implement several interfaces. Now, one thing I'm confuse about. If Class A implements interface A, if we instantiate Class A, since it's implementing interface A, it also have the "same type" as interface A? I'm asking that because I notice in an example:
C#
Person person = new Person();
DoSomething(Person)
static void DoSomething(ISomething something) {...}
C#
Person person = new Person();
DoSomething(Person)
static void DoSomething(ISomething something) {...}
Sometimes, the object person is passed as an argument to the method DoSomething while it expects the ISomething type as argument and not a person argument. But it works, why is that please. Finally, can someone explain the use cases of interfaces and their importance please. For example if we don't create interfaces, say I have 3 classes: Animal, Dog and Cat. What I did is, I create a "general" method in Animal, say MakeSound. Dog and Cat extends Animal so MakeSound also exist in each respective classes. Then I would be able to override the MakeSound method just like we kind of would do in an interface. So what's the difference of implementing an interface or extending a class here please
13 Replies
Angius
Angius4w ago
Basically correct, yeah
mg
mg4w ago
Sometimes, the object person is passed as an argument to the method DoSomething while it expects the ISomething type as argument and not a person argument. But it works, why is that please.
Because if Person implements ISomething, then everything that's on ISomething is on Person, so a Person "is a" ISomething
Faker
FakerOP4w ago
yeah I see so basically, it's the same concept of inheritance, right? Like if we have a class called "Animal" and a class call "Dog" which inherits from Animal, then, a Dog is an Animal ?
Angius
Angius4w ago
One example from a project I have, would be an IMailer interface, and 3-4 different implementations using different email providers. Then, I just request an IMailer, since I don't care which service I'm currently using, I just care that I have a .SendEmail() method Sorta. I like to think of classes in terms of is, and about interfaces in terms of can Say, you have your animal example. Bird inherits Animal and has a .Fly() method. You make a Parrot, it can fly, you make a Stork, it can fly, you make a penguin... uh-oh So you use some IFlying or ICanFly interface instead
Faker
FakerOP4w ago
oh ok, didn't think of it like that, that's a great way of doing it. So the idea with the Animal and Bird is that if we make Class Bird inherits Class Animal, this would mean all birds should be able to fly while this may not be true. While with interfaces, you can define sort of a template method, then define whatever implementation we want later on in case it's a penguin for example? (but then the question arise :c, if Parrot is a bird, Stork is a Bird, Penguin is a bird; they would be all categorised from the class Bird. But then this class would implement ICanFly, right? how to make that we restrict a particular bird no fly while the others fly? Using method overloading but then, can't we do that with classes? 😭 )
Angius
Angius4w ago
Just implement ICanFly in the birds that can fly class Parrot : Bird, ICanFly class Penguin : Bird
Faker
FakerOP4w ago
ahhh I see something like that
C#

interface ICanFly
{
void Fly();
}

class Bird : Animal { } // Base Bird class has no Fly() method

class Parrot : Bird, ICanFly
{
public void Fly() { Console.WriteLine("Parrot is flying!"); }
}

class Stork : Bird, ICanFly
{
public void Fly() { Console.WriteLine("Stork is flying!"); }
}

class Penguin : Bird
{
// No Fly() method because Penguin does NOT implement ICanFly
}
C#

interface ICanFly
{
void Fly();
}

class Bird : Animal { } // Base Bird class has no Fly() method

class Parrot : Bird, ICanFly
{
public void Fly() { Console.WriteLine("Parrot is flying!"); }
}

class Stork : Bird, ICanFly
{
public void Fly() { Console.WriteLine("Stork is flying!"); }
}

class Penguin : Bird
{
// No Fly() method because Penguin does NOT implement ICanFly
}
Angius
Angius4w ago
Yep, basically
Faker
FakerOP4w ago
yeah I understand now, how inheritance differs from interfaces, the idea is that, using inheritance, what Class A inherit from say Class B, Class A can do everything that B can. Using interfaces, we can restrict that though, like instead of defining a fly() method in class B, we can implement it as an interface where only specific birds will be able to fly (please correct if I'm wrong)
Angius
Angius4w ago
Yeah And you can implement multiple interfaces per class Which you cannot do with inheritance
Faker
FakerOP4w ago
yep noted I now have a clearer concept, thanks !!!
Angius
Angius4w ago
That ICanFly can be now reused on Mosquito, for example. And some ICanSuchBlood can be implemented both by Mosquito and Bat And so on
Faker
FakerOP4w ago
yeah I see

Did you find this page helpful?