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

implements

Learn how to tell a class to implement an interface i AS3. In the last video we created an interface and in this one we will define what objects are a type of that interface and what are not.

Video Transcript

00:00And if we had more functions, we could list all of them out.
00:02Now, every time, if we try to create a new class,
00:07and we try to implement it,
00:09it must adhere to that touch function.
00:12So let's see how do we actually make sure
00:14that we integrate that, for example,
00:16into our cup of water first.
00:18So because our cup of water was already inside
00:21of the polyformism folder, all that's left for us to do,
00:24the class could continue and extend
00:25whatever it needs to extend.
00:27But it could also implement,
00:31so we say implements, then I touchable.
00:34And what we just did right now is we told our construct,
00:38we told our, what did we tell?
00:41We told our compiler more than anything else.
00:43We told our code that the cup of water class
00:47is implementing the I touchable.
00:50And all that really means in our scenario
00:52is that the function touch is implemented within here.
00:55Now, obviously, in the real world,
00:57you'd probably have more than just one function.
00:59There would probably be multiple functions
01:00that are being implemented most of the time.
01:03So now that we know that our interface
01:05implements the I touchable,
01:07we can now treat it as an I touchable.
01:10We don't have to even treat it as a cup of water.
01:13But let's see that right after we add inside
01:16our animals as well to implement the I touchable.
01:19Now, again, we have to implement the I touchable.
01:21To do that, I'm gonna have to import the I touchable.
01:25So that's inside of our com folder,
01:27and our zero to geek,
01:29inside of our ope, inside of our polyformism,
01:32inside of our I touchable.
01:34And again, all we have to do is a space
01:36right after extends movie clip,
01:38and say extends I touchable.
01:43Now, notice that this time around,
01:44and let's just make sure I don't have any typos,
01:46this time around, I set my extends I touchable
01:51not on the dog class, not on the cat class,
01:53but directly on the animal.
01:54And because I have inheritance,
01:56the dog and cats should be already adhering
01:59to the I touchable because the animal is.
02:01Let's see if that's really true or not, all right?
02:04So let's move on back into our main class.
02:06We've done, we've been traveling a lot.
02:08We created the I touchable.
02:09Very, very simple, right?
02:11It's an interface, has a name,
02:13and has just a function that says it implements that function.
02:17Now, and actually, we don't even need to do this
02:20opening and closing bracket.
02:22It's really semicolon.
02:23You just define those functions,
02:25you don't create their body.
02:26So let me just save that because that wasn't needed.
02:28So really, you would just add here
02:30as many functions as you need.
02:31In this case, in scenario, in our scenario,
02:33we're just gonna have the touchable.
02:35Now, let's go back to the main.
02:36And this time in the main, I don't wanna talk to animals.
02:41Instead of that, I wanna talk to our polypharmism,
02:43I touchable, and literally, all I'm gonna do right now
02:47is I'm gonna ask, is our target implementing I touchable?
02:53And literally, when we ask if it is,
02:57it doesn't have to, it's not that our class is I touchable,
03:00that that it implements I touchable is good enough.
03:04And let's give it a run and see what happens.
03:05First of all, let's fix errors.
03:07So I'm gonna double-click here
03:08and go to wherever my errors are.
03:10So let me see, what am I doing here wrong?
03:13Save text error expected left brace before extend.
03:16Oh, because I did extends twice, right?
03:18So implements, that was bad, right?
03:21So classes are extend,
03:23implements are for implementation of interfaces.
03:26So let's run it again, see if we have any more typos.
03:29Excellent, no more typos.
03:30If we click onto our fire,
03:31no, our fire is not an I touchable.
03:35If we click on our water, our water is touchable.
03:37If we click on our dog, our dog is touchable.
03:40If we click on our cat, our cat is touchable as well.
03:44Leads us to everything that's not touchable
03:47because it doesn't implement I touchable,
03:48it's not I touchable.

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!