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

Polymorphism in action

Its the grand finally. time for us to take everything we did so far from creating 3 separate classes that have the same functionality through implementing them to follow our interface ITouchable now is the moment we all waited for its time for us to create our first polymorphic moment!

With our classes implementing the ITouchable interface its time for us to create together the first time together a Polymorphism.

Video Transcript

00:00Why did we create all this all this lead on all this last 40 minutes of build up to this moment?
00:05We're really for this next step. That is really amazing
00:08So we have here inside of our click inside of our main class right now
00:12We have here a click that's we're just tracing but we what we could do is ask if it is
00:17touchable we could tell our E dot target and I want to cast it because I want to make sure that
00:24We treat it as that so I could take and you know
00:28Let me do it without casting first so you can see why I want to cast it because really I want to call the function touch
00:32But if I call the function touch one, I'm not getting the hints to if I run my application
00:37I'm gonna get an error. Maybe when I click on it
00:40No, no errors today. It's working perfectly without any issue, but you know what?
00:45Sometimes you'll get an arrow error on other things so I want to show you why and how we could work with it
00:52by basically saying to my E target casting it
00:56To be touchable
00:58So what I'm basically doing is I'm wrapping it inside of the touchable making sure and it's not like creating a new element
01:04All I'm doing is I'm casting it
01:06I'm telling the compiler that this is an eye touchable element and now that I know that it's an eye touchable element
01:12I'm gonna get out so those hints for my function and I'll be able to get them more easily
01:16Although it's a little bit more code, but if you add there a couple of function
01:19It would be very functions. It would be very helpful and I'm not 100% sure about this
01:24But I think if you run this inside of flex or other builders, you might get errors if you don't define it explicitly
01:31explicitly
01:33So let's run it one more time and
01:39Again, we'll see that fires we could click on and nothing doesn't happen
01:42Even though the click event is happening because it doesn't adhere to eye touchable
01:46It nothing happens if we click onto the water now our water will work
01:51I'll water will drain itself up all the way until it reaches the final frame and then it will not be clickable anymore
01:57It's not gonna drain anymore and our dog will bark when we click on it and our cat will
02:02Get annoyed at us if we click on it a lot
02:04and that's really all you need to know about the heart of interfaces, there's quite more about interfaces, but
02:12It's a good time at this moment to take a short break breathe in
02:15Understand what we just learned and then we'll continue and add a little bit more complexity. Not that much

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!