‫In this video, you are going to learn about inheritance in C sharp or in programming in general.
‫I'm going to try to keep it super simple because it can become quite complicated if you look at it because
‫there is a lot going on.
‫But I'm going to simplify it as much as possible because the idea of inheritance is really the reusability
‫of code.
‫And this allows us not only to reduce our own code, but reduce code of other developers or even of
‫other namespaces directly without having to write a lot of code manually again and again and again,
‫which was there before.
‫And this really reduces repeated or redundant code.
‫So the idea of inheritance is basically it's a fundamental concept of object oriented programming.
‫It allows you to define a child class that reuses or inherits, extends, or modifies a parent class's
‫behavior.
‫The class whose members are inherited is called the base class.
‫The class that inherits the members of the base class is called the derived class.
‫Okay, so what does this mean?
‫Well, in simple terms, if you have more than one class with the same code or functionality, but it
‫differs slightly, we can actually put this repeated or redundant code in a separate class and then
‫make our class this inherit from it.
‫This code can be method's functionality, so to speak, or properties which are the attributes of our
‫classes.
‫So let's look at an example.
‫And therefore I created a new project here, which I call Inheritance Demo.
‫All right, so let's go ahead and look at this example.
‫So usually when you are working with multiple classes, you go ahead and create a new class here.
‫So let's do that here as well.
‫You could, of course, create the class in the same file, but that's not good practice.
‫So let's go ahead and call this class radio.
‫All right.
‫So here's the class, say radio ad.
‫And now we have this radial class, which is in the same namespace.
‫So both classes, the program C is where we have our main method, which is the starting point of our
‫application and the radial class are in the same file.
‫So we're going to have two classes.
‫One will be radio and the other one will be TV.
‫Let's just assume we have a couple of properties.
‫So for example, a boolean that determines the state of the radio is it turned on, and then the brand
‫of the radio.
‫So is it Panasonic or whatever other brands there are?
‫And then we have a simple constructor here, so you should definitely have an understanding of the core
‫concepts of classes.
‫If you want to fully understand inheritance, that's generally something that I would recommend understanding
‫before getting to inheritance because it's building on top of each other.
‫Okay.
‫So then I want to have two methods because this is basically going to be the functionality.
‫So one is going to switch the radio on, it will be the switch on method and the other one will be the
‫switch off method.
‫So you can turn the radio on and off or switch it on off using these two methods.
‫And then we will have another method which will allow us to listen to the radio.
‫So we have this method here called Listen Radio and it will check.
‫Is the radio turned on right now?
‫So is this is on property here, this boolean, is it going to be in fact true?
‫And if that's true, then we can listen to the radio and the way we are going to indicate that we are
‫listening to the radio will just be with this console red line statement saying listening to the radio
‫and otherwise we're going to print this error message saying radio is switched off, switch it on first.
‫Okay.
‫So before we can listen to it, we need to turn it on.
‫All right.
‫So that's our radio now.
‫Now, imagine you have another class, which is going to be.
‫The TV.
‫So let's go ahead and create a new class.
‫And therefore you can go over here to add and then add class and give this class a name.
‫I'm going to call it TV.
‫And now in this class, I will go ahead and have a couple of properties similarly to what we had here.
‫So basically, I'm going to also have the state if it's turned on or off.
‫So let's add those two properties in here.
‫Then I will have a simple constructor which allows me to set those values when I'm creating a new object
‫of TV.
‫So here pull is on as well as string brand, which sets the properties.
‫Then I will have the individual other methods so I can turn on the TV and turn it off similar to what
‫I did in the radio so I can basically copy this code.
‫But it will be slightly different because we had the comments which said radio here, but now this will
‫be TV.
‫So this method will turn on the TV and this one will turn it off or switch it off.
‫So then we just need to have another method which will allow us to watch the TV and it will do something
‫very similar to what our radio does.
‫It allows us to watch the TV only if the TV is on.
‫If it's off, we will need to turn on the TV first.
‫Okay, so now we have these two classes and if you look at them, they are awfully similar.
‫So there is a lot of stuff that is exactly the same, basically because this property is on as well
‫as this brand, they are the same.
‫We have them in both classes, so basically we have redundant code.
‫We had to copy and paste a bunch of code and it's more or less the same.
‫So how can we make it so that we don't have to copy and paste a lot of code and rewrite code and basically
‫have redundant code?
‫Well, that's the whole point of the concept of inheritance.
‫So inheritance comes to the rescue.
‫We can solve this issue by creating a third class called electrical device.
‫This will contain all of the shared functionalities and properties of an electrical device we have.
‫So let's look at those.
‫What are the ones that we have that are the same?
‫So what are the properties that are the same for our TV as well as the radio?
‫Well, it's in our case all of the properties, actually, but it doesn't have to be all of them.
‫In this case it will be because we don't have more properties.
‫But the two properties that we have that will be the same.
‫So is on and brand are both properties that exist in TV as well as in radio.
‫So our new class should have those two properties and then we have the two methods which allow us to
‫switch the TV on, the radio on or the radio off and the TV off as well.
‫So for you, a little exercise if you want to try this, because I assume that you are not a pro and
‫programming at that point, but you will feel a lot more confident when you try this.
‫So go ahead and create a class that has all of these properties and these methods that we can or that
‫are shared between the two classes, create them in a separate class and call this class electrical
‫device.
‫Actually, you can call it however you want, but I'm going to call it electrical device like such.
‫Okay.
‫So I hope you passed the video and tried it.
‫So I'm going to create a new class and put a way, of course, you see, you can use the shortcut control
‫case, so let's do that.
‫In this case, Control K C and this will open up this window here and let me change it to electrical.
‫Device like so.
‫And now this class will have the properties that we are sharing between the two other classes.
‫So let me put them in here.
‫The boolean that determines the state of the electrical device, is it turned on or not?
‫And then the brand of the device.
‫And then of course, we need to have a constructor.
‫So electrical device is on.
‫And Brent and finally we have the functionality which are methods.
‫So whenever you have functionality, you put them into methods and the functionality is to turn something
‫on or turn something off.
‫So whenever there is an action happening with your class or with the object that you are using, you
‫will need to use methods.
‫And that's what we're doing here.
‫So we're using a method to turn something on, in this case the electrical device, and to turn it off
‫or switch it on and switch it off.
‫Okay.
‫So that's basically what we're doing here.
‫So now that we have our shared properties and methods in one class, let's make the other class of the
‫TV and radio classes inherit from this class or in other words, extend this class.
‫So this is just a different term that you can use here.
‫So our TV and our radio will now extend the electrical devices class.
‫So how can we do that?
‫Well, let's start with the radio, because the TV will be done.
‫A little challenge for you.
‫What?
‫You will have to do it yourself.
‫So here you use the colon and then the name of the class that you want to inherit from electrical device
‫is the class that our radio now will inherit from, or our radio will extend the functionality of the
‫electrical device class.
‫Okay.
‫So Colon and then the name of the class that you want to extend.
‫And what's important is, is of course, that this class has to be either in the same namespace or you
‫have to add the namespace up here.
‫So in our case, we're not using any of those, we're just using system, but as we are in the same
‫namespace, so the electrical device says class is in the same namespace as radio and TV.
‫We can just do it like so, but now we can see that our boolean has a warning here.
‫So when you hover over your ID, so Visual Studio is going to tell you Radio DOT is on hides, inherited
‫member electrical devices is on use the new keyword if hiding was intended.
‫So basically we are using the same property name and here you want to avoid that because you have this
‫property, so to speak, already because you're inheriting it from the electrical device.
‫So now you can just hide it or override it with this one that you have created here if you want to use
‫something with the exact same name, but it should behave somehow differently, then you can do that.
‫Even though that's not good practice necessarily depends on the use case, of course.
‫So this is because now the radio class contains two properties of the same name we are hiding or covering.
‫That's a different term here.
‫The properties that we inherited from the electrical device class.
‫So we will remove the redundant code that we don't need anymore.
‫So let's get rid of that.
‫And of course the redundant code is not just assigning or creating the properties but also having these
‫methods here.
‫So let's get rid of those as well.
‫Now we have another error that appears here and that is this radio constructor that seems to have an
‫issue.
‫It says there is no argument given that corresponds to the required formal parameter is on of electrical
‫device, electrical device, pool string.
‫So what is this electrical device or electrical device?
‫Pool string Well, it's this one here.
‫It's this constructor.
‫Since our properties are coming from the base class and our base class contains a constructor, we can
‫use the base keyword to pass our base class.
‫It's constructor arguments because these are the only two properties we have and our constructor in
‫the child class will have an empty body.
‫So that is a lot of talk.
‫But basically what this means is because our radio constructor isn't any more complex than the base
‫one, we can just go ahead and say base and here is on and brand.
‫Like so.
‫And then we don't need a body for our radio.
‫So this constructor will basically say please pass the is on as well as the brand properties that are
‫passed to the radio object that when it's created pass it over to the electrical device constructor
‫because basically our radio is inheriting from electrical device and therefore we can do that so we
‫can just pass it over to electrical device.
‫And what that will do is it will basically set the properties is on as well as brand based on the values
‫that are passed.
‫So this is.
‫The more complicated part.
‫But the power here is really that we don't have to build redundant code and well, the work is done
‫for us.
‫So it really saves us a lot of time and writing code as well.
‫In a later example, we will see how to use the constructors body in the child class to initialize its
‫own unique properties.
‫To just quickly reemphasize the radio class here.
‫Is our child class.
‫So the inheriting class and the electrical device is our parent class.
‫So the base class.
‫So we are going to inherit.
‫So our radio inherits from electrical device.
‫So the child inherits from the parent.
‫Basically, your parents have a certain DNA and you have the same DNA, but you have a little more to
‫it, so to speak.
‫So that's how you could think about it.
‫All right.
‫So now a little challenge for you.
‫Adjust the TV class so that it inherits from electrical device as well.
‫So pause the video and try that, please.
‫All right.
‫I hope you tried it.
‫So first we need to add colon electrical device.
‫Then we can get rid of the redundant code.
‫So the repetitive code, because we have it in our base class already, and this means that we can use
‫it in our TV class directory as well.
‫And now we can also adjust this here, saying that we're going to pass the data on to our base class.
‫And of course, we don't need to set that here as well.
‫But as I said in the later example, we are going to see how we can still use the body efficiently and
‫that is going to be basically it.
‫Now we can just go ahead to our program CSV file and actually check our code here.
‫And by checking our code, I mean create objects of our radio and test it.
‫So let's go ahead, create in radio object.
‫I'm going to call this one my radio and this will be a new radio object.
‫Now, of course, I need to pass whether it's on.
‫So I'm going to turn it off at the beginning and I'm going to say it's going to be made by Sony, so
‫the brand will be Sony.
‫Now I can access the properties.
‫You can see here this tool item, the properties of my radio, which is the brand and the is own property
‫and I can also access IT methods.
‫So switch on, switch off, listen radio.
‫And then you can also see that there are some methods that we never created ourselves which are just
‫there from the get go, because every class inherits from the object class and the object class has
‫a two string method as well as a get type get hashCode and equals method.
‫So these methods, they are there for every single class that you are creating in C sharp because basically
‫we don't see it, but internally it inherits from the object class like so.
‫So every class inherits from the object class and that's why we have these different methods.
‫But now let's first of all switch it on.
‫So here, switch on.
‫This will switch on our radio and now we can listen to our radio.
‫So hear my radio.
‫Dot, listen.
‫Radio.
‫Now let's run this and this will allow us to listen to our radio.
‫So that we are listening to the radio.
‫So we switch to radio on.
‫Let's now not switch the radio on and listen to radio.
‫And it will say radio switched off.
‫Switch it on first.
‫All right, so now let's do the same thing with our TV.
‫Let's go ahead and create a TV object.
‫I'm going to call this on my TV, which will be a new TV object, and this one will be turned off as
‫well.
‫And it will be a Samsung device.
‫And now let's go ahead and use my TV dot switch on to switch it on.
‫And then let's call the watch TV method.
‫Mike.
‫So now let's run this and we will see that now our TV is turned on and we're watching it while listening
‫to the radio.
‫So quite a task there.
‫But basically our code does what we want it to.
‫So now you see we call the switch on method for the radio as well as the TV.
‫Even though if we look at the TV class, it doesn't have this method, so it doesn't have a switch on
‫to well, switch on or switch off method.
‫The radio doesn't have those methods either, but that is because they are inheriting from the electrical
‫device which has those methods switch on and switch off that we can use it.
‫So basically here we're using the method of the.
‫Base class.
‫So here.
‫Method of base class.
‫And here we're using the method of child class.
‫So this switch on method comes from the parent class and the watch TV method is from the child class.
‫So now we can create the code that is unique to the class, and it's not repetitive, so to speak,
‫directly in the TV class as well as in the radio class.
‫But everything that is the same and will be relevant for electrical devices in general is something
‫that we can create in here.
‫There is a lot more to inheritance and of course we're going to look into all of that throughout this
‫chapter.
‫So see you in the next video.