Java Polymorphism Fully Explained In 7 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you've been taking a java class or any object-oriented programming class you've probably heard of this thing called polymorphism and you've probably been told that it's really important that it's one of the four core concepts of object-oriented programming but the word just sounds really complicated right what exactly does that mean a lot of classes and books over complicated so in this video we are going to make polymorphism super simple my name is john and i put up a new java tutorial video every single week so be sure to leave a like and hit the subscribe button so you don't miss each week's video i also have a full java course available in the link down in the description if you're interested if not awesome i'm thrilled to have you here and let's get started polymorphism what does that word mean well it really just means many shapes or many forms poly many morphism forms so you're probably thinking okay it means many forms cool but what exactly does that mean to me in java that just means that your classes can do the same thing in different ways i think it's easiest to talk about what exactly polymorphism means in java with an example so i've got a very simple piece of code here where i have an animal class and here i say animal my animal equals new animal and then i call this method on my animal eat myanimal.eat and we can go over to the animal class and see what that eat method does and all it does is print out the word munch to the console so we can go back here and run our program and of course it just prints out the word munch but now let's say i've also got this dog class and my dog class is a subclass of animal it's a child class so here i've got public class dog extends animal and as you probably know when you create a subclass of another class you automatically get all the fields and methods of that parent class so what does that mean here this eat method that's in the animal class i don't have it written in the dog class but because dog extends animal i can go back to my main method and i can create a dog my dog equals new dog and i can call my dog dot eat you can see that that method is available even though we don't explicitly have an eat method in our dog class and we can go ahead and run this program as well and we'll see our animal eat and our dog eat and they both print out munch but now here's where polymorphism comes in even though this dog class automatically gets that eat method and i don't have to do anything by default it has the exact same behavior in other words if i don't do anything my dog is going to eat in exactly the same way as any other animal it just prints out munch because i didn't specify anything else in my dog class but what i can do here in this dog class is override that eat method from the parent animal class and if you want to override a parent method you have to implement the exact same method signature as that parent method so we can see the method signature here is public void eat it's a public method it doesn't have a return type so it's void the method name is eat and it doesn't take in any parameters so over in our dog class we want to implement the exact same method signature to override it so we can just have public void eat and open and close the curly braces what this allows us to do is implement this eat method in a different way for dog than it does for other animals so instead of munch we can say system.out.printline oh maybe chomp chomp we can save that and go back to our main method where we have a regular animal eating and a dog eating but now this dog will eat in a different way than the generic animal eats let's run our program and see what that looks like our animal says munch but our dog says chomp chomp at its core that's what polymorphism is in the java world it's when you have a method in a child class like our eat method in our dog class overrides a method in its parent class our dog classes eat method is overriding the functionality of the eat method in the animal class and because of that we're now doing the same thing in a different form so that's polymorphism our dog class eats in a different way than the animal does our animal class gives a certain default functionality for eating but our dog child class overrides that functionality and does the same thing in a different way and you can have other subclasses that do the exact same thing so i've also got this cat class that also extends animal it's a child of the animal class also back in our main method we can create a cat my cat equals new cat and even though in our cat class we don't yet implement that eat method because we're a child of animal we have it available my cat dot eat and again we can run our program and now our basic animal says munch our dog says chomp chomp because we overrode the eat method from the animal class but when the cat eats it also just says munch because it's not overwriting that eat method yet but we can do that same thing in our cat class public void eat and instead of munch or chomp chomp we can say system.out.printline nom nom nom save that go back to our main method and run our program again and now our animal says munch our dog says chomp chomp and our cat says this is just more polymorphism we're doing the same thing in three different ways many forms we have our parent animal class and we have our dog and cat classes that are both child classes of animal and all of these classes implement that eat method but they do it in a different way same thing in many forms polymorphism now there is one other form of polymorphism in java and that's called method overloading now it's really easy to confuse method overloading with method overriding method overriding is what we already talked about where you have a child class that implements the exact same method signature as a method in the parent class but you can also do what's called method overloading to put it very simply all that method overloading is is when you have two methods in a class that have exactly the same name but take different parameters for example here we have a public void eat method that takes no parameters now of course java won't let us just copy and paste that method and have it twice because we can't have the exact same method with the same parameters but what we can do is have exactly the same method name but with different parameters for example this version of the eat method could take in an int and we'll call it like number of times now you can see that java is no longer complaining about a duplicate because even though these two methods have the same name they have different parameters and that is allowed that is called method overloading of course if you do method overloading like this you would want the different versions of your method to do something a little bit different so because this takes in an int of number of times maybe this method can print out chomp chomp the number of times that is sent in this parameter we can do that with a simple for loop where we have four and i equals zero i less than number of times i plus plus that's the number of times we can have it print out chom chomp so let's save that go back to our main method and instead of calling the eat method without any parameters let's call the eat method that takes an in parameter and we can send in just three and run our program again and now we see that chomp chomp printing out three times but if we want to that regular eat method that doesn't take any parameters is also still available to us so that's another form of polymorphism in java method overloading that's where in one class you have multiple methods with the exact same method name but that take different parameters if you enjoyed this video or learned something please be sure to leave a like and hit the subscribe button so you don't miss the new video every week i really do appreciate you taking the time to watch subscribe and share it's the only way these videos get out to help more people so thanks again and i'll see you next time
Info
Channel: Coding with John
Views: 13,226
Rating: undefined out of 5
Keywords: polymorphism in java, java polymorphism, polymorphism java interview questions, java polymorphism interview, codingwithjohn, coding with john, coding with john polymorphism, polymorphism java, polymorphism object oriented programming, java, object oriented programming, object oriented programming java, java polymorphism tutorial
Id: jhDUxynEQRI
Channel Id: undefined
Length: 7min 16sec (436 seconds)
Published: Sun Apr 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.