I I don't know how to begin these videos. Sometimes I say "hello, we're back" Repeating myself. Okay, so, but this is a video about - edit that out - about polymorphism. "poly-morphism" Polymorphism sounds like a Fancy word. It's gonna be really complicated topic. This is the third principle of object-oriented programming and it's actually gonna be something hopefully quite simple for us to understand. So we saw hint of this, by the way. before. We had this particle system - which managed particles. Then we made this "square" particle class, which was a child of particle, And we were able to put that in the particle system and everything just worked How did that happen? Well, let's think of a similar scenario. Let's say we have an array Okay, well, let's actually just say for a second, we have a class called "Animal" and let's say we have a class called
"Dog" and "Dog" extends Animal and we have a class called "Cat" and Cat extends Animal and we have a class called "Turtle" and Turtle extends animal. Right. You can see where this is going. We could have this base Animal class like our base Particle class and lots of subclasses that have all the basic functionality and data of an Animal, but with their own customization. Now what we want to do is make an array, I'm gonna ...in our, in our Particle system example, we're gonna use ArrayLists, but just for thinking it through right now, let me use a plain old array. We're gonna make an Animal array called Kingdom King dom Which is a new Animal array, and you know, we're gonna have a thousand animals in it, okay? I want to put Dogs and Cats and Turtles into that array. Can I do that? The answer of course is yes! Otherwise why would we be here? But just think back to... Would not have been your first instinct? Maybe you would have said, I'm making a pro-state sketch and have all these different objects on the screen. I need my Dog array to keep track of all my Dogs. I need my Cat array to keep track of all my Cats. I need my Turtle array to keep track of all my Turtles. This is gonna get unwieldy pretty fast. In some cases you might want that, but the hope ...one of the reasons for doing this inheritance structure is it allows us to, kind of collect all of these Objects that-that-that-that have the same parent together and work with them together. This is polymorphism. This is going to... Polymorphism is going to allow us to put all these different kinds of Animals into the same generic array of Animal objects. Array of generic Animal objects, I think is what I meant to say. So let's look at two Lines of code that I'm going to write out for you here. I'm gonna say Dog Dod ... Dog rover equals new Dog Okay, this is something we should be very comfortable with. We're very comfortable saying I have an object of type Dog It's name is Rover. It's a new Dog object. Great. Here is polymorphism - something new that we're allowed to do. Animal spot equals new Dog. This is polymorphism. What polymorphism, which means from the Greek, "many forms" - what it allows us to do is say I have a Dog object and if, well, if it's convenient for me to think of it as a Dog object I'm happy to do so. If it's more convenient for me to think of it as an Animal object, that's also allowed. I mean it really truly is a Dog. A Dog is a Dog is a Dog is a Dog. But we can talk about it as if it's an Animal because a Dog IS an Animal. It's a child class. The Dog is made from a child class of Animal. We can make an Animal object which is specifically a Dog. This is what allows us to put Kingdom Index 0 for example could be a new Dog. Kingdom Index 1 could be a new Cat. This is the magic of polymorphism. Kingdom is just a bunch of Animals. A Dog is an Animal, a Cat is an Animal. We can collect all these different kinds of classes into the same array. Great right? Okay, so hopefully this makes sense to you. But there's one little piece of this that I think would be worth mapping out. What happens when we loop through that array? Right, and we know now, by the way, that we have this new kind of loop that we can do. We can say "for every Animal a in the Kingdom array, I could say, and what do our Animals have? They had a sleep function and an eat function. I could say "a dot sleep", "a. eat" So I could still loop through all of the Animals and when it gets to each one, I don't have to say if it's a Dog, sleep like a Dog, if it's a Cat, sleep like a Cat. That's the magic of polymorphism, and inheritance is it just knows how to do it It knows like we made a Dog and put it in the first spot We made a Cat and put it in the first spot. They're both animals, but a Dog is a Dog is a Dog is a Dog, when it gets to sleep It's gonna sleep like a Dog. When it gets to eat, it's gonna eat like a Dog, so this is very powerful. We can have a generic... we can have an array of generic Animals. We can put specific kinds of Animals in that array. We can loop through all them and we don't have to keep track of which one is which. Java / processing / the .... you know ... the computer...the little people inside the computer Pulling in all the switches and buttons in there, it keeps track of all that for us. This is incredibly powerful. It's going to allow us to make a particle system full of particles Square particles, Spinning particles, Star particles, Rainbow particles And we don't have to keep track of what's what we can just put whatever we want in it We can say they're all particles and the program is going to keep track of that for us Okay, I hoping this makes sense to you. If not We will remedy that situation somehow, but to let's actually go now back to our particle system example. I forgot to turn the timer on so I don't know how long this has been but I think it's a good amount of time and let's see polymorphism in action. okay, so if you remember We actually have polymorphism in action right here because we are putting Square particles in the ArrayList But the ArrayList type is a generic Particle, and we can see here We're treating a Square particle as a Square particle when we want to make it But as a regular Particle when we're accessing it, because this is us looking at it in different forms. So this is probably more physical right here in action, but let's really prove to ourselves that this is working And I'm going to add just a little piece of functionality here. I'm gonna pick a random number between 0 and 1 and I'm gonna say if that random number is less than 0.4 There's a 40% chance of me adding a square particle And there is a 60% chance of me adding a regular Particle So what I mean by this is I am not keeping track of which particle is square which particle was random I'm just randomly adding. Sometimes I'm adding a Square particle, sometimes I'm adding a regular Particle, and when I loop through the particles They're all just particles But when I call display the Square particle is going to know to be a Square the regular Particle is going to know to be A Circle. And when we run this, we can see here some Square. 40% of the time we've got a Square particle 60% of the time, I don't know why I picked those probabilities, we have a circular particle. This is polymorphism. This is very powerful and what I would suggest to you as an exercise is Make a whole series of particle classes that are all slightly different, that all inherit the main Particle with some extra variables, with some extra functions that do slightly different things, look a slightly different way, and put them all in one big particle arraylist and make your magical Confetti whatever explosion happen on the screen and Sing to yourself and applaud and be really happy so We should we should be happy while we're doing this. If you're not happy doing this , really there's lots other things you can go and do. Unless I guess someone's requiring you to watch these videos which nobody should and ever. Anyway ok? Hooray, this is the end of this video about polymorphism Write some comments, leave me questions. Send me an email and We're basically finished with particle systems. I'm gonna look at a couple more topics, and I think they're gonna have two more videos So I'm gonna make but I'm gonna stop talking. I'm gonna hit this button