02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

In Between super And override

In this video we explore impotent concepts in inheritance and OOP - 'super' and 'override'.

Video Transcript

00:00So far so good. We created that animal class that worked for us really brilliantly for our three different animals
00:07But in the real world every animal is going to have some of its own unique personalities
00:11So just the dog might bark when we click on it and the cat might
00:17perk its tail when we click on it and maybe our
00:21Rabbit will do something else. Let's start with our dog. So let's create a unique solution for our dog
00:27Now there are a lot of ways to to solve this
00:30Inheritance is not always the perfect answer and really inheritance is is really useful when you really have a group of functionalities
00:38That are unique for that specific element in this case
00:43We're gonna use even though maybe won't be the perfect scenario. We're gonna create a dog class to do that
00:49I'm just gonna go and I want to make sure that I'm using the same package
00:52So I'm just gonna copy the package so I don't have to type it again
00:55And I'm gonna click on file new and I'm gonna create a new action script 3 class
00:59And I'm just gonna type calm that everything affiliate ope dot inheritance dot dog
01:07Fantastic and now that we're ready know how to use the extends. I actually want to extend the animal
01:12I don't need to import anything because our dog class is in the same package as our animal
01:18So all I have to do next is in my dog class after it was already created for us automatically I could click on extends
01:28Entype animal now if you don't have if it didn't automatically type it in then at this stage
01:34You should be already more or less comfortable creating your own classes
01:38Alright perfect. So this stop at this scenario. We have our class we have our dog
01:44Everything is brilliant so far. No problem, right?
01:46We could save this file and we could call our dog
01:50Save it in the right folder and we have a new dog class
01:54what I want to do next is I actually want to go into my
01:57Inheritance line and I want to go to my dog and I want to change it from an animal to be a dog
02:03So I'm gonna go to the properties and I'm gonna change it from animal into a dog
02:08Now notice that all these changes even though I have another class aren't gonna change really any functionalities and that's fine
02:15They're not gonna change any functionalities if I run my application one more time and I run it one more time
02:21We'll see that our dog is still happy when we click on it
02:24Now the reason is is because we didn't really add or change any functionalities in the extension of the dog
02:31But let's change a few stuff. Let's add a few new properties for one our dog will only
02:37Let's add here something else
02:40Let's add one more variable and this variable is going to be available in the animal class. We're gonna call this variable
02:46It's going to be a private var again
02:48And this private var is going to be a bullion that tells us if the animal eats
02:54well, if the animal is happy when touched is
03:00Happy when touched
03:03Which will be a bullion and that value will be false by default
03:07So most animals when you touch them, they're not that happy
03:12And we're gonna make a few more modifications
03:14So first of all react let's instead of calling it. Let's instead of calling it react. Let's call it
03:21Touch all right. So react could be touch will change the name to touch
03:26touch just to keep that new idea in our head and
03:30Make sure that we call that function touch as well
03:32That's all we had to do and everything will continue working because this is all bounded in the same place
03:37We're not calling touch in any other place besides here. All right, so we have our new variable
03:42What I want to do is I want to add into my into my touch function a question if I'm happy when I'm touched
03:49We'll do one thing. If not, we'll do another thing. Okay, so if
03:55If is happy when touched we're gonna do one thing. Otherwise, we're gonna do another thing
04:02So the first thing will be I'm happy to see you
04:05that's if we are happy to see them which happens when is happy when touched and
04:10The other one will be
04:13trace
04:17not that happy
04:20All right, so
04:22So far so good. Perfect. So we have your a dog. We have your a cat
04:25We have your rabbit when a dog gets touched
04:28He's usually really happy and we want to make sure that we could change that is happy value to be true
04:34Only problem is is because it's a private value
04:37If I try to go into my dog and I change that is happy when touched if I go into my dog
04:43Constructor and I type that value and set it to be true that value is not gonna work
04:48It's not gonna work. It's gonna come back and it's gonna give me an error telling me
04:51Well, I'm trying to access saying that I cannot access
04:55Even though our dog inherits properties from the animal the private variables are hidden from the dog as well
05:02So we could have set the variable to be public, right?
05:05If we set the variable to be public then is happy when touched will be visible to the dog as well
05:11but only problem with that is
05:14Now the whole world could touch it now anyone could go in our main or any other place and change the value is
05:21Happy when touched but we want to make sure that only inside of our package only inside of our class only inside of this
05:27relationship only whoever should have access will have access and for that there's one more type of
05:34Accessor that we need to learn about we learned about private we learned about public
05:38And the next one is the one that's in between and it's called
05:43Restricted or not really it's called
05:47Reserved no, but we're getting warmer, right?
05:51Protected so very close, right? So it's basically what I said restricted protected the word is protected
05:58That's the keyword and when we use the word protected that saves our variable and makes sure that only whatever it has
06:05Rights to access it will have rights taxes
06:08So in this scenario our animal has access to it nothing really changed yet, right?
06:13But well, maybe something did change actually if we run our application now when we click on our cat
06:18our cat is not happy when we do that when we click on our dog our dog is very happy and very pleased and
06:24That was done by our inheritance when we inherited we made sure that we could change a property now one thing that is
06:32common to do and important to do
06:35Because our initially in our animals file
06:38We have all this logic here that we want to make sure that will be called even though it's automatically called
06:43Traditionally, it's important to add the word super in the beginning of your function
06:48What that says is basically call the constructor of the ancestor before you do anything else
06:54So first of all call the super constructor call and initiate everything that's inside of this function and only then afterwards
07:01Carry on and do whatever you want to do inside here
07:03now if you wouldn't put it it would be put in by default anyways, but we want to put it in because it makes it clear and
07:09Easier for us to see that that
07:12Relationship is actually happening
07:15Alright, so we have our dog and their dog is happy when when it's seen now the next step that I want to do is
07:22I want to actually create a different reaction completely
07:25so first of all the dog this is all we want to change in the dog the dog is happy when it's seen and
07:31Now though the next step would probably be is that I'm not happy
07:35Maybe we should have more personal dog ish reaction, which I'm not happy is not very dog like right now to do that
07:42We'll probably have to override the touch function
07:46To override the touch function is quite easy the way we override the touch function is by using that word
07:53Override and then writing the exact same enclosure exact same signature. So this is called a signature
08:00So I'm going to copy that function signature and take it in into the dog class
08:05Now I can't just do this. This is this is not valid
08:08I can't just override a function that already exists if I do that
08:12I'm gonna get a lot of all these weird errors basically telling me that
08:16Overriding a function that's not marked with override is not permitted
08:20To do to enable and basically the reason is is we want to make sure that we tell flash listen
08:25We want to override that function. So we'll type override
08:31Override so override public function and then our function we could override it and we have no problem if you run our application one more time
08:38We'll see that now when we click on our dog the override works perfectly
08:42We have no issue
08:44Really the next step that we want to do next is well, even though we want to override it
08:50I want to keep what we had there. So first of all, let's let's create our own logic here
08:55So whenever a dog is such he's always happy, right? He's always happy. Not only that. He's always happy. He's going to bark
09:02So bark bark bark
09:05Bark bark bark. So there we go. So when you click on the dog he barks barks barks when you click on the cat
09:11It's he's not that happy wise cuz cats by default
09:15It's is not isn't happy when touch is set to false every animal is that way and our touch happens
09:21But what happens if we want our dog both have its unique touch
09:26Interaction, but we wanted also to call the super what happened before how could we access that function that was above and again?
09:33That keyword is the way we're gonna use it. So
09:35What we have to we're going to have to do is type super
09:39dot
09:40the name of the function and
09:46We just trigger it. Now the beauty of this is that super is busy
09:51Is basically has two roles one is super the functions of the constructor when we're inside of the constructor and super is
09:59After that constructor was called then we have a super property of object. That is actually that
10:05Object that's being created the ancestor the step above where we could interact with this with the parent that was above
10:12If we run our application now
10:14We'll see that when we click on our dog
10:16Our dog is going to bark bark and is going to be happy to see us now we if we wanted to we could
10:21Just change the position. So if we wanted to we could have had him be first happy first do what's above first
10:27Take care of what's inside of the touch and only after that bark, so I'm happy to see you bark bark bark
10:32Alright, so so far so good
10:34So now we are extending that touch and then we're calling this touch above. So this is quite complex
10:40There's a lot of stuff that we're learning in this little little piece of code
10:45Next step after this is really art is all we really need to know in the basics of working with extension
10:50So now we know how to in our constructor call the parent constructor making sure that our constructor
10:56Isn't solo. We know how to override a function. What if we want to have
11:01Unique functions for a dog. Let's say we have we want to have a unique function for a dog
11:06That's easy enough because we already done it before so we could create a public function
11:10and this function will be
11:13Lick yeah, why not function lick void and when lick the dog will
11:25Licking you
11:27How brilliant?
11:29Alright, so our lick function
11:31Maybe our lick function will only be called when our when our dog is directly approached and asked to to be licked
11:39Alright, so this touches our dog
11:42Extension now
11:44Technically we could extend also our cat and we could extend also our rabbit but really for the sake of our sample here
11:50We've just wanted to show how we have our dogs separately our dog has its own kind of implementations on top of the basic
11:58Implementations our cat is using the basic implementation and our rabbit is using the basic implementation as well
12:05Let's take it one step further
12:07Now inner one step further these two elements don't really have a timeline and don't need a timeline
12:14But our rabbit does have a timeline it has a timeline and it's a big one
12:20Now we have a problem with that because we're not really using that timeline because we set it as a sprite that never runs
12:26Our wiggle never happens
12:28so for our wiggle to actually happen we're going to have to treat our rabbit as
12:32A movie clip and if we treat our rabbit as a movie clip then all the other
12:36Elements will be movie clips as well and the way we do that
12:39We could go directly into the animal class and just say instead of it extending a sprite
12:44We could tell it to extend a movie clip. So to do that, I'm going to import the movie clip instead
12:50And I'm going to extend the movie clip instead and now all of my elements are now moving up
12:58So if I run my application one more time
13:00We'll see that automatically my rabbit is going to wiggle his glasses
13:03Which I don't really want him to do that
13:04So let's extend the rabbit and add a few more functionalities just for the rabbit itself
13:10Alright, so let's do that so we could copy the dog cuz we let's do it for scratch actually just because it's a good practice
13:17So we're gonna copy again the package. I'm gonna go and create a new class
13:23And I'm gonna type rabbit and in my constructor. Well, first I want to make sure that I'm extending
13:32And extends
13:34All right, we're extending but we type extends
13:37animal and
13:38Again, our animals in the same package. So we have nothing to worry about
13:42We can now save the file making sure that we're saving it in the right package all the way down in the inheritance
13:48package and
13:50In my constructor, I want to do one thing because now we're a movie clip
13:53I want to tell my rabbit stop stop the playhead don't automatically play anymore
13:59Next thing that I want to do is I want to make sure that whenever my rabbit is clicked
14:03It's not gonna care if it's happy not happy or anything like that. Whenever it's touched. It's gonna wiggle its glasses
14:10To do that again. I'm gonna have to override the touch function. So I'm gonna type override
14:17Override
14:18public function has to have the exact same signature and
14:21It's not a bad idea to copy it because you don't want to make any mistakes
14:25You could always copy that function paste it inside making sure that your signature is exactly the same thing
14:30And in this case, I want to go to and
14:33play
14:35wiggle
14:38Well, it's this axial label. So let's just go to that label just for one second before we actually do it
14:43So you can see what I'm talking about. We have your wiggle label
14:46Right in our properties of the label wiggle
14:49So we want to go to that ramp and you want to tell it go to and play you know that it's a string wiggle
14:56And
14:57Last but not least we want to make sure that we do actually have us
15:00We actually do want to have a stop because if we wiggle right after we wiggle now
15:04It's going to automatically wiggle each time because now it's going to play in a loop endlessly
15:09To prevent that from happening. Maybe instead of actually putting anything inside of my rabbit. We could just call a super
15:15so there's nothing unique in our rabbit constructor, but in our
15:24Timeline itself. I'm just gonna put on the keyframe itself stop
15:28So I'm gonna go and I'm gonna type stop
15:31That way each time we meet that first frame the stop command will call
15:36So even after we animate if it goes back to the first frame, we will stop
15:40So let's check and see if this is working
15:42So what it should be doing is whenever our animal whenever it triggers an event and on click touch happens
15:49And when that touch happens our rabbit is overriding a wiggle
15:53Last thing that's left for us is to make sure that we're actually extending in our
15:58Library that our mat our rabbit our logo our rabbit logo will extend that class
16:03So let's make sure that it is instead of animal
16:06We want to tell it no instead of animal you want to tell it from now on you're going to be extending the rabbit
16:12And we're gonna click ok and ignore the messages click enter and let's check it out
16:20So first of all we have our dog and our dog will bark when he's happy to see us
16:23our cat is just not gonna be that happy to see us well our
16:28Rabbit is gonna wiggle its glasses each time we click on it

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!