Polymorphism in Typescript [IN UNDER 6 MINUTES]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this video we're going to see what is polymorphism and how to implement it in typescript so by definition polymorphism is when we have multiple classes that have the same method but a different implementation of that method and the polymorphism makes use of inheritance let's go ahead and see it into action let's say we have a class called animal and this class has one method called make sound the body of that function is console.log animal sound let's go ahead and create another class which is dog this time which inherits from animal so in typescript we say extends and then the base class name so if you take a look here we have a different class a second class which is called dog which extends which inherits from animal so animal here is a base class and dog is a child class whenever we have an inheritance the class that is inheriting from the base class inherits all of its methods and properties so let's say here we had a property called name which is of type string dog also would have a property called name let's go ahead and copy paste this function right now so as you can see we have the same method but here let's change the implementation so instead of animal sound let's say bark i'm going to go ahead and create a third class i'm going to call it cat which also inherits from animal so it also has the make sound by inheritance the make sound method but again let's change the implementation so let's say meow if you look at if we take a look at what we have so far we have three different classes animal being the base class which has the method make sound dog and cat are child classes that inherit from animal as well so they they inherit the make sound method but if we take a look at the method implementation as we can see every single one of these three classes have a different implementation here we have animal sound here we have bark and here we have meow let's see polymorphism into action let's say in our program we have some function called make animal sound and this function takes one parameter we're going to call the variable animal and then which is of type animal is class so of course we have access to this method right here so here i can call i can call the make sound method on the animal instance now usually what we do is let's say we have that animal equal new animal so i'm creating an instance of that class let's now call this function our pro in our program make animal sound and the animal that i'm gonna pass is animal of course here we could have called it anything else like animal one for example if i run the program as you can see it printed animal sound which is this now let's say we had another animal let's say animal 2 for example equal new dog this time so we created an instance of the dog class let's say instead of animal 1 i'm going to pass in animal 2 at the argument and run the program as you can see in the console now we have printed bark so if you take a look here if you take a look at the class dog the make sound implementation prints bark although here in this function our program we do not know which type we are actually invoking we just know that it should at least inherit from animal or it should be animal now let's do this again let's say let animal 3 or we can call it cat if you want equal new cat and also let's this time call the same function but passing the cat so i'm going to save to run the program again and as you can see we have printed meow so here in this function we're just saying that the parameter is of a type animal which is the base class and every class that inherits that base class of course they inherit the properties and the methods that they have but since we have a different implementation we have a different behavior as you can see whenever we call this and we pass in a different type so the first time we had animal sound the second time we had bark and the third time we had meow so we have the same method but different behaviors depending on the type of the class that inherits from the base class now let's say we don't have the make sound implementation here we just have an empty class which inherits from animal of course we have the make sound class inherited from animal but we did not change the implementation so the default implementation the default behavior is going to be executed so now when we call the make animal sound on cat i'm going to go ahead and save again as you can see it called the animal sound which is the method the main the default implementation and the base class animal since we don't have a different one so to conclude we have seen how in polymorphism we have a single action which is make sound but that has different behaviors and does the same action in three different ways based based on the object that we are sending so when we are sending a normal animal it's executing the default make sound implementation when we are sending a dog or a cat it is executing the implementation of that specific object of the class thank you for watching if this video was helpful please leave a like and subscribe and if you have any other notions or concepts that you would like to know please leave a comment and i'll make a video about it thank you and see you next time
Info
Channel: Computerix
Views: 3,416
Rating: undefined out of 5
Keywords:
Id: k6PHl6xQXV0
Channel Id: undefined
Length: 5min 54sec (354 seconds)
Published: Fri Apr 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.