Our first Interface
The steps involved to create an interface are very easy. The most important thing to understand is what an interface actually is and what its used for.
Creating our first Interface. In this next video we will explore what interfaces are and how to work with them in a live example that connects the dots between all the work we did so far.
Video Transcript
00:00We know that both animals have,
00:03that animals have a function that's called touch,
00:06that dogs have a function called touch,
00:08that cats have a function called touch,
00:10and so does a cup of water have a function,
00:13has a function that's called touch.
00:15Now, what I wanna do is I wanna create an interface,
00:17so I'm gonna create a new file,
00:19and our new file is gonna sit
00:20inside of our polyphromism folder,
00:23so let me just, you know,
00:25we'll put it actually in the base folder
00:28of object-oriented programming.
00:31And now, we'll put it into polyphromism,
00:35because that's really our lesson, right?
00:37The core of our lesson.
00:38So again, I'll create a new file.
00:40It's this time around,
00:41I'm gonna create a new ActionScript 3 interface.
00:43Notice that we're not going for the class,
00:44we're going for the interface.
00:46Our interface name is gonna be that full path
00:48of our directory path,
00:49plus the name that we want for our interface,
00:51and I and many people like calling interfaces
00:54starting with a capital I for interface,
00:57and then the name of whatever it is.
00:59And I wanna create an I touchable.
01:02And really, the idea itself is
01:04that we have an element that is touchable,
01:07and I'm gonna click on OK.
01:08So now, notice that I'm not creating an extension,
01:11or I'm not creating anything that's related,
01:14or even closely related to inheritance right now.
01:16What I'm doing is, first of all,
01:17we have our package,
01:18which is the exact same format as we had with classes,
01:21but then we have a new keyword,
01:23which is called interface,
01:24so I wanna create it on my own.
01:26I don't want someone else creating it for me.
01:28So the way we do it is very simple.
01:30We create a public interface,
01:33and then we name the interface the same name
01:36that we call our file,
01:37which is gonna be I touchable.
01:40We're gonna open a curly bracket,
01:42close a curly bracket,
01:43and then we're just gonna save that file,
01:45making sure that we save it into the correct spot,
01:47which is gonna be inside of the polyformism folder,
01:50and we're gonna make sure that we call it I touchable
01:53with a capital I and a capital T.
01:57Contrary to classes,
01:59interfaces enables us to list out the public functions
02:03that are mandatory to be for a class
02:07to adhere to that interface.
02:09And really, the idea of it is taking it to the real world,
02:12imagine that to be able to enter a club,
02:15you have to have clothes from Ralph Lauren.
02:18I don't know, depends on the club, right?
02:20So if you don't have clothes from Ralph Lauren,
02:23then you can't enter this club.
02:26Really, that's very limiting, right?
02:29Because then only Ralph Lauren clothes
02:30could be okay for this club.
02:33But what if clubs could enable clothes manufacturers
02:38to follow a set of rules for clothing
02:43that enable people to enter their clubs,
02:46or for example, their restaurants.
02:47So let's say you have to wear a tie,
02:49the suit has to have a tie,
02:50they have to have buttons,
02:52the clothes must cost more than $500,
02:54and if they don't,
02:55you have no right to enter this building.
02:58So the idea itself is the club itself
03:01is not the creator of the clothes, right?
03:05Sorry for scratching my nose live, but it happens.
03:08So let me just have a sip of water.
03:13Oh, wow, that is excellent.
03:16Scratchy nose.
03:17All right, so we're saying we have a club or a restaurant,
03:21it doesn't really matter.
03:22And in that restaurant or club,
03:24they don't create clothes, but they do have an interface.
03:27They do have a rule that says only if you're one, two,
03:29three, four, you could enter our club.
03:31And that's exactly what an interface is.
03:33An interface, all it really does is it creates a rule
03:36that says whoever wants to become an eye touchable,
03:40or someone that implements eye touchable,
03:43my nose is so scratchy.
03:45I have to scratch it.
03:48Ah, it's like an ant went into my nose.
03:50Okay, so anything that is eye touchable would then adhere.
03:55So in our case and scenario,
03:57you don't define public in these type of stuff.
03:59You don't write full functions.
04:00You can't put variables either.
04:02Very, very simple.
04:03Interfaces, all you do with an interface
04:06is you list out the functions that are going to be
04:09the interface that whatever class must implement.
04:14Now, it has to be public.
04:17So if a class implements that interface,
04:20that function must be public,
04:22because it's the exact same level.
04:23When someone goes to the bar,
04:25the bar needs to be able to see those clothes.
04:27It needs to be able to have access to them.
04:29And it really, in that level, it's that deep.
04:32So let's create that function.
04:33It really is the only one function that is touchable
04:36throughout the different functions here.
04:37So let's call it.
04:38So our function name was actually touch.
04:42We then put its full signature.
04:44It doesn't have anything in it,
04:45and it doesn't return anything.
04:47And there you go.
04:48We just added our first function into our first interface.
