Object Oriented Typescript #4 - Inheritance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey YouTube welcome back to my object-oriented typescript series in this video we'll go over inheritance so in the very first video we had an introduction with object-oriented programming and I introduced you to a lot of concepts and the developer Bob who was working on the object-oriented solution this was his final solution where we had a player class and this player class is called a superclass because we have four other classes inheriting from the player superclass so the player is called a superclass and Mario Luigi toad and Peach are called sub classes and player the player class has the move method and playsound method there should be two parentheses right here the player class has those two methods defined and whenever a subclass inherits from a superclass its methods get inherited so Mario Luigi and toad move and play their voice recording normally so they don't have to re-implement those methods but because Peach has specific special behavior to the peach class we can override the method right so in this example a peach was flying and she also had a different type of voice file wav file compared to the mp3 files we can override player's methods by read Eclair the methods and having peach specific behavior the first benefit that we'll talk about when it comes to inheritance is removing code duplication the second benefit is flexibility which we'll go over in the next video with polymorphism going back to the first benefit with inheritance which is removing codification code duplication is something that we always always want to remove in our code bases because code duplication makes our code very error-prone for example if we have code in three different places or four different places like here the move playsound methods what happens if we wanted to change the move behavior for all classes we would have to update each and every class which is very annoying and more so we could forget to update one specific method along class and then we would have a buck because one one player would be moving differently than the other players and we do this in programming everywhere right you do this with loops you don't go up to our code editor if you want to print you know number four one to ten you're not gonna do console dialog 1 2 and so on right you're gonna write a loop I'm gonna write a for loop I'm gonna go all the way to 10 and then you're gonna print out I write this removes code duplication and you also do this with functions let's say if you want to do this over and over again you're not going to copy pieces five times you're just gonna write a function print nums that does this and then you can call print nomes five times instead right this removes code duplication we have function reuse now going back to the classes inheritance is the idea is the same idea that we want to remove code duplication we want to pull out and abstract all the common methods shared amongst these subclasses into the superclass right and then once we link the four player classes with the superclass then they can inherit the super classes methods so in this video I'm going to work with a new example instead of the mario clone i'm going to let's pretend that we're going to create an animal simulation so let's create an animal class we're gonna have to need animals in our game and let's think about let's ask the question what does an animal know because when an animal knows is its state and hence its instance variables it knows its hunger which let's say is our rating out of 10 so it's gonna be a number it knows its health and let's say this location we have the x coordinate and the y coordinate and then what is its behavior whoops let's change that to why what is the behavior so an animal can eat it can sleep let's say it can move and it can make make a noise and let's implement these methods for all animals so basic I'm eating I'm sleeping and for move as you can guess I'm moving and for make noise make noise and now let's create so now that we have animals we can create we can we can create subclasses great our animal is the superclass and we can now create subclasses of it so let's say we want to create a dog and we want to create a cat how do we do the the link right we draw the arrow from the set from these subclasses to the superclass how do we link them and we do this with the keyword extends so we have class dog extends in animal right this is this is inheritance a dog is inherent in everything from the animal so the dog extends the animal the cat extends an animal as well so to show you that dog and cat are indeed inheriting everything let's create a dog your dog and let us call dog right and we have to dot operator we can already see that all the intellisense we have access to the animals instance variables and methods because the dog is inheriting all of them so let's make some noise and dog let's go into our let's go into our terminal and we get make noise so the dog we want to have specific behavior to the dog we don't want this general make noise method so what we do is override the make noise so and the way we override the method is is just by reading them and we can constantly dialogue mark so let's compile and let's run the j/s and we get bark C and for comparison let's create a cat alongside the dog and let's make a noise on the cat and we'll see that the cat because we didn't overwrite anything still inherits the regular make noise behavior but the dog has specific behavior because it override in it so we have barked and make noise we can also make these methods more complex they don't have to be basic console dialogues let's say this return to owner method we want to print let's say its current position right we can access these instance variables because the dog has inherited these these men these instance variables and here if you want to print out its position first before we turn it to owner we could we can do we can do that so I want to print out I am at this and we have see we have access to all of the super classes instance variables and methods as well so we can let's say I'm right here I'm at this duct cord X and this dot cord why and now I'm returning to owner right now here for the dog we have to set X&Y first and you know from the previous video you now know that doing something like this is not good because it breaks encapsulation so instead we'll create getters and setters so for this example we don't really need getters per se all we need to do is set them so then this method return to owner can access them so we're just going to create set coordinate X this is gonna take in the next it's gonna be number and what this is gonna do is set this dot cord X equal to X and do the same thing for y obviously this entire cost is not encapsulated but for the specific example you will encapsulate D variables the instance variables this is y and court y equals y and of course we have no logic to check if x and y are valid so this is not an this is not different from having public variables but for now we'll just roll with this but just know that this is not really encapsulated because we still have access to set them to whatever we want so right here we're going to remove this and we're gonna set dog dot set corn into X - 10 and we'll do the same thing with Y and here is the first thing that you'll see is an error why is the dog we have type errors it says property cortex is private and only accessible within class animal so when I first told you that the subclass inherits everything in instance variables and methods I wasn't telling the entire truth the superclass the superclass is private variables do not get inherited by subclasses if there's if it's private we do not have access to it so there's another keyword that types group has which is called protected and when protected is is the same thing as it's similar to private as and we cannot access these two variables outside of the animal class but when we inherit when we create subclasses and we can inherit from the animal class we can still access this outside the animal class so I'm gonna collapse this animal class right here we can still access the two protected instance variables from the animal class from dog so we have no more type errors so that's also a new keyword that you will learn and then let's run t.j.s and of course I forgot to call a method so right here let's call a dog dot return to owner and great we get the behavior that we're looking for I'm at ten ten and now I'm returning to owner so this method you can see that we can we can override methods with make noise we can create new methods that art that don't exist on the superclass and we can also access its super classes instance variables with this and we also got introduced to the protected keyword which is similar to private but allow its subclasses to access its variables as well so going back to why we use inheritance the first reason that you're being introduced to you right now in this video is removing code duplication right we can create many objects we can create cats we can create dogs we can create wolves right and you can see here let's say I wanted to create more animals like wolf equals new wolf right all we have to do is create a wolf class that extends the animal and we don't have to provide any extra code right because all the code exists in the animal so then I can create cats and wolves and dogs but if I do need to to house a specific behavior then I can override it by just really clearing the methods and another benefit of inheritance is that it allows us to model our code according to how we think in the real world right so two key words to terminologies that you'll hear around you that you'll hear when working with object-oriented programming is is ax and has ax right so we see that when a dog extends an animal a dog is a animal right because a dog is an animal so whenever you have the extends keyword the left hand side is a right hand side and we also have the has relationship so an animal has hunger it has health it has an x-coordinate and it has a y-coordinate and a dog here we don't have anything created here but we can create new instance variables as well we can let's say a dog has an owner right which is a string a dog has a owner so is ax and has ax are two relationships that you need to be comfortable with when working with inheritant and object-oriented programming and there's a specific test that you can use to make sure that your code makes sense and your hierarchy of classes and super classes and subclasses and that's called it is a test you can say out loud is a dog an animal a dog is an animal and that makes sense then your code is probably fine but if it doesn't let's say like a dog is a let's say furniture let's say you're extending from some furniture class that makes zero sense right so you're not modeling your code correct so that's one test that you can use to make sure that your code and the way you're thinking about the real world is correct the last thing that I want to introduce to you is let's just delete all this code below here and we still have the animal class is inherited two levels down or multiple levels down right you don't have to stop at the first level so let's say we had a canine class right so we have a class canine and extend an animal k9 is an animal and we know all canines can bark let's say right or we all we all let me know that yeah we know it barks so we're gonna override the make noise method we're gonna read Eclair so console dot log bark bark bark but then we don't have to stop here we can also extend the canine so we can say dog extends canine right and when a dog has is let's say it has an owner which is a string and if I want to let's say a dog has a method called return to owner and this just prints out returning to Oh templates make template strings returning to this dot owner and here that's the dog class I'm gonna collapse that I can also have a wolf class right so you can see it with this inheritance that we can we can keep it going we can model hierarchies that appear in the natural world with inheritance right so the dog class will have its behavior so let's say and we can see it has all of the inherited behavior and how does its stuff declared a dog it has the make noise method and it has all of these methods and instance variables from the animal right dog right return to owner and owner were belonging to the dog class make noise we could also write because it belongs to the canine because a dog is a canine and we can also get access to let's say eat sleep and move because the dog is an animal so we also have access to eat sleep and move because since a dog is a canine right a dog extends a canine so a dog is a canine and a canine extends an animal so canine is an animal then we get that a dog is an animal - it's a canine and it is an animal so inheritance can flow through multiple classes right a canine is an animal and a dog is a canine so a dog is a canine but it's also an animal so I forgot to talk about an important concept when it comes to inheritance that's similar to overriding methods and that's extending method so this should really come after the overriding example but if I edit it this example inside in between there it's not gonna make sense because then it's gonna like disappear because I built onto that example but let's do this it should be simple so I just pasted some the animal class and this one is simple it only has an X and y coordinate and it has they are private and we have two setters that don't really do anything to encapsulate because we have no logic but that's just for the theoretical point I'm trying to make and we have two main methods so this is just either just two setters and we have two methods make noise and move make noise it's over vided by the dog so we create a dog new dog and we call it dog make noise this will print out bark bark bark right so we completely override it the make noise method right this there's this line will not be executed let's say we wanted to instead of conferred the move behavior we wanted to just build on top of the behavior of the move method that the animal class already provides we don't want to completely override it or erase it so we can access the animals class with the keyword called super so let's say we're creating the move method and we first want to print out let's say getting up on all four paws and then we'll print out the animal classes move method which is just I'm moving from cord something-something right and what we want to do is this behavior builds on top it extends the animal's behavior it extends the animals move method here's the new stuff that we want to include and this is the animal's behavior so the way we access the superclasses class it's just with the keyword super because it's a superclass and what we can do is we can call super dot move right and before that so this is going to print out this whoops this is gonna print out this and then when we want to print out this we can just simply remove this and log this right here so let us test this out we're going to move open our terminal compile this and then run it in j/s and we can see the first example the first line bark bark bark we are completely overriding the super classes make noise method but in the second when we invoke the move method instead we can see that we are extending the animals move method right we first log our custom behavior that we want for the dog getting up on all four paws and then we call super dog move and of course here it's undefined because we forgot to set them so let's do that let's do dog don't set let's do dog dot set cord x equals 65-54 I'm getting tired and this one's 48 this is y right so let me compile it whoops that's not gonna work compile it and then let's run it with node you can see that the move method correctly extends the behavior right so when you are dealing with subclasses and you need subclass specific behavior you have two options you can completely override or you can simply extend and access the superclass with the super keyword so yeah that's it I'll see you in the next video
Info
Channel: Jeff Zhang
Views: 5,594
Rating: undefined out of 5
Keywords: Tutorial, typescript, typescript tutorial, javascript, javascript tutorial, es6, es7, es8, es9, es10, es2020, web development, programming, coding, programming tutorial, coding tutorial, objected oriented programming, objected oriented programming typescript, oop, oop tutorial, objected oriented tutorial, objected oriented programming tutorial, inhertiance, object oriented inheritance
Id: Taku4OukEZ0
Channel Id: undefined
Length: 20min 56sec (1256 seconds)
Published: Fri May 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.