Java polymorphism 🏁

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody it's your bro hope you're doing well and in this video i'm going to teach you guys about polymorphism in java so sit back relax and enjoy the show if you find this video helpful please remember to like comment and subscribe your support will help keep this channel running polymorphism this is a greek word for poly meaning many and morph meaning form or forms it's the ability of an object to identify as more than one type think of it as being identified as more than one data type here's an example let's say we're going to have a race and we can enter in all different sorts of vehicles into this race they don't all have to be the same so we have a few additional classes a class called vehicle vehicle is going to act as the parent class for my class car so car extends vehicle bicycle bicycle extends vehicle and boat boat extends vehicle so car bicycle and boat are all subclasses or children classes of the vehicle superclass so let's create an instance of our car bicycle and boat luckily through the magic of copying and pasting i have done that already we have a car object a bicycle object and a boat object so let's store well at least attempt to store all of these objects within an array called racers bracers equals and then we will place our car bicycle and boat within this array so what's the data type of this array going to be well we're going to have a problem could we make this cars well we can store our car object within an array of cars but not our bicycle and our boat the same thing goes for bicycle we can store our bicycle but not the car in the boat and lastly with the boat data type we can store the boat but not the car and the bicycle and to solve this issue we can use the power of polymorphism so for example our car identifies as a car but since it extends the vehicle class it also identifies as a vehicle same thing goes with our bicycle and our boat so one thing they all have in common is that they are all vehicles so we can change the data type of this array to vehicles and therefore we can store our car our bicycle and our boat because all of these objects also identify as vehicles so if you're going to store objects within an array and they're all different object types you would have to find what they have in common and make an array of that type so that's how to store different objects within an array using polymorphism and now for our wrists we're going to call a go method that we're going to define within each of our classes car bicycle and boat so let's begin with car let's create a go method so let's make this public void it's not returning anything and this will be called go so when we call this method all we'll do is have a print line statement system.out.printline let's say something like the car begins moving okay let's repeat the process for bicycle and boat let's change car to bicycle and within the boat class car to boat so each of these methods well each of these classes has their own go method and we could call them individually kind of like this car.go bicycle.go and boat.go so this would work but there's actually a better way to write this and what would be better is if we were to create an enhanced for loop to iterate through all of the elements of this array of vehicles so the data type for this enhanced for loop is going to be vehicle we'll use x as our counter colon and then the name of the array which is racers so this is going to iterate once through all of the elements of this array of vehicles called racers x represents the vehicle we are currently working with so x dot and we would like to use the go method so our enhanced for loop doesn't care what kind of vehicle you are if you're a car a bicycle or a boat it cares that you're a vehicle and if you're a vehicle use your go method whatever kind of vehicle you are so there's one more step to this too the method go is undefined for the type vehicle so we need to make sure we have this go method within our class of vehicle and i just generated that here so what we're doing is actually method overriding within the car the bicycle and the bulk class so this isn't necessary but it's considered good practice to add the add override annotation to each of these methods and then our enhanced for loop is going to iterate through all of the elements of our array of vehicles called racers so it doesn't care what kind of vehicle you are if you're a vehicle use your go method well luckily our car our bicycle and our boat all also identify as a vehicle so that's all that polymorphism is it's the ability of an object to identify as more than one type think of it as identifying as more than one data type our car does identify as a car but it also identifies as a vehicle same thing can be said with our bicycle and our boat but not only that all objects are children classes of the object class so they also identify as objects so you could write this using the object data type but there's a few other changes that we would have to make so that's all that polymorphism is it's the ability of an object to identify as more than one type so if you would like a copy of all this code i will post all of this in the comments down below but yeah that's the basics of polymorphism in java hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you
Info
Channel: Bro Code
Views: 9,209
Rating: 4.9937501 out of 5
Keywords: Java polymorphism, Java polymorphism and inheritance, java polymorphism example, java polymorphism tutorial, Java, polymorphism, polymorph, tutorial, explained, example
Id: 2hkngtWLGvE
Channel Id: undefined
Length: 6min 12sec (372 seconds)
Published: Tue Oct 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.