JavaScript Beginners Tutorial 24 | OOP | Polymorphism with example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome I am Raghava and today we will learn about polymorphism in JavaScript so in JavaScript we have already learnt about object-oriented programming concepts like encapsulation and inheritance and today we will see what is polymorphism so in simple words poly means many and for mouth means form so polymorphism means many forms or the process of representing something in different forms so let us take an example I will go to my visual studio code and I will create a new file and I will name this as polymorphism dot GS and here I will just create a class called animal and in this class I'm going to create a constructor and this constructor will take the name as the input and here I will say this dot name equals name so I am setting the name variable and I also have a function called let us say eat and here I will say in this function I will just print out I'll say name this dot name and eats food so this is a very simple class with a simple function if I create an object for this class I will say let animal equals new animal and I will just name this as dog and if I now call the function eats let us see the output if I run this it the same dog eats food so this is working fine now let us say I create another class and extend the animal class so I am doing inheritance and we have already learnt about inheritance so I will say class alligator extends animal and here in this class I am again creating a same function called eat and here in this class I will say console dot log this dot name eats and I will say fishes now you can see this two classes have the same function eats and this is a child class and this is a parent class now suppose I create our object for the child class I will say let Morphy equals new educator and i will just pass on the name Murphy and now I will use this object to call the function eats and let us see what happens I will clear the output and I will run this do you see what it does it says Murphy eats fishes so it has actually used this particular function and not the parent function now suppose I do not have this function here I'll just comment this out and I will now run this so it says Murphy eats food so if there is no eats function in the child class it looks into the parent class and runs this function however if the function is present with the same name in the child class the eats function get overridden so this function gets overridden by the child class function the child class function takes preferences and this is also called as method overriding so this method gets overridden the parent class method gets overridden by that and class method and that is how we are getting this getting executed and here just in case I want to call the parent class method as well I can just say super dot eats and let us see the output now if I run this now you can see it is running Murphy its food and Murphy eats fishes so this calls the parent class method eats and this then execute whatever is there in the child class also if you see the constructor is also being used from the parent class if you want to create its own constructor you can something like this you can say constructor and say name and just call the super constructor which is the parent class constructor however there is no need to define something like this it will already take the constructor from the parent class so this is now you can see this function eats is now present in multiple forms and we can decide which form will get executed based on the object we create and then whatever we put here so this is more polymorphism so in object-oriented programming it is the ability to create a variable a function or an object that has more than one form so this was a very simple example I will suggest that you look at some more examples and do some more hands-on with polymorphism example so that this concept is very clear to you and I will meet you in the next session of JavaScript thank you for watching
Info
Channel: Automation Step by Step
Views: 9,517
Rating: undefined out of 5
Keywords: polymorphism in javascript with examples, what is polymorphism in oop, javascript object oriented programming concept polymorphism
Id: XxMUJ9-Pb2E
Channel Id: undefined
Length: 5min 31sec (331 seconds)
Published: Thu Aug 01 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.