All right.
So we have seen three of the five applications of interfaces.
So first of all to promote code reusability and flexibility, then decoupling code and enhancing testability.
And in the last few videos we looked at dependency injection, which was quite a topic I have to say.
But then number four is something that we're going to cover later on with Yannick.
And number five is what I want to cover in this video.
So achieving multiple inheritance.
So where would we use multiple inheritance when we need a class to implement multiple functionalities
okay.
And why would we do that.
Well to allow a class to conform to multiple contracts and when.
Well in our case in the full multifunction device that can both print and scan.
So we're going to build a little software that will allow us to print and scan it.
This will show us that we how the concept of multi inheritance or multiple inheritance can be achieved
in C sharp with a workaround through interfaces.
Right.
So we saw that we cannot inherit from two classes at once, but we can inherit from multiple interfaces
at once.
So let's look at how this works.
So let's build it.
Therefore I will have a public interface called I printable.
And then it will just print okay.
It doesn't do much more.
And then I have another public interface that will be I scannable and that will scan void scan okay.
And then I have another class which will which will be a multi-function printer okay.
So public class multi-function printer which implements the I printable interface and the I scannable
interface.
And this is just an example because it could potentially implement a bunch more.
So I'm just using two for simplification.
And here in this class in the multifunction printer, that's where I'm going to say how I actually print
and how I actually scan.
Because now this printer is inheriting, so to speak.
Not really, but it's implementing interfaces multiple.
So it's doing multiple inheritance to a degree.
So here I can now say how the implementation of my print method should be and how the implementation
of my scan method should be.
Okay.
And now we're just going to say scanning document here for example.
And in the print statement we're just going to say printing document.
All right.
So now let's go ahead and use this inside of our program.
And therefore I'm going to create an object of my multi-function printer which I'm going to call printer.
And I'm going to use this printer to print.
And then I'm going to use the printer to scan.
There we are.
So now let's run this and we will see that it says printing documents scanning document.
So this was a simple example of how multiple inheritance can be achieved.
Because this multi-function printer class now implements I printable and I scannable.
So both of these interfaces now in our case these interfaces just have a simple method.
But it could have a bunch of properties as well and many methods.
Same goes for the I scannable interface.
It only has one method, but it could have plenty more.
So now you could say obviously you could also just make a printer class which does all of the things
that the printer does, but architecture wise it would make it a lot more difficult because let's say
later on you want to have a printer that can only print, that doesn't scan, but then you would have
all of this overhead, and that's where these interfaces come in handy.
Okay.
Now the cool thing at the same time is that we have flexibility now because there's multifunction printer
can now be used anytime that we need a I printable object, or any time that we need an I scannable
object, we can use the multifunction printer because it is, so to speak, inheriting from both.
So it is implementing both, and therefore we can use it as if it was an I printable object where it
behaves like an I printable object.
And sometimes it can behave and I can I scannable object depending on where you need it and how you
want to apply it.
Right?
So that's where again polymorphism would come into play and so forth.
But generally speaking this is how you achieve multiple inheritance in C sharp.