1
00:00:00,990 --> 00:00:07,020
So it's time to turn our attention to a concept that might be entirely new to some of you and some of

2
00:00:07,020 --> 00:00:09,780
you may have seen a similar concept in other languages.

3
00:00:10,050 --> 00:00:15,960
And that concept is interfaces and interfaces can be challenging to people the very first time that

4
00:00:15,960 --> 00:00:18,180
they encounter them, but they're really not that hard.

5
00:00:18,360 --> 00:00:24,840
And I think the easiest way to demonstrate what an interface is is to actually build one and implement

6
00:00:24,840 --> 00:00:26,400
that implemented a couple of times.

7
00:00:27,120 --> 00:00:32,370
In really simplest terms, an interface is nothing more than a kind of contract.

8
00:00:32,370 --> 00:00:37,960
An interface says, OK, any type that uses this interface has to follow these rules.

9
00:00:37,980 --> 00:00:40,320
So let's let's let's just actually build one.

10
00:00:40,330 --> 00:00:41,370
That's the simplest approach.

11
00:00:41,380 --> 00:00:47,880
So I'm going to start by defining an interface and I define an interface in much the same way I do,

12
00:00:48,870 --> 00:00:50,010
in much the same way I do.

13
00:00:50,010 --> 00:00:54,240
One of the custom data types that we looked at a couple of years ago, and it's you start with the word

14
00:00:54,330 --> 00:00:58,410
type, keyword type, and this is going to be a type of some name.

15
00:00:58,410 --> 00:01:00,570
And I'm going to call my interface animal.

16
00:01:01,650 --> 00:01:06,330
And it is of type, not struct was as we saw when we were creating the user type.

17
00:01:06,330 --> 00:01:07,920
It's a type interface.

18
00:01:08,670 --> 00:01:13,530
And then I have curly brackets and inside I have this signature for that interface.

19
00:01:13,530 --> 00:01:22,020
And all the signature is, is it lists the functions that an interface, an interface of type animal

20
00:01:22,170 --> 00:01:23,070
must have.

21
00:01:23,250 --> 00:01:29,230
So anything that is of type animal must have, it must implement some of some functions.

22
00:01:29,250 --> 00:01:34,320
So I want to say one function that every animal type must have is says, what does the animal say?

23
00:01:34,860 --> 00:01:37,710
And it's a function that takes no arguments and it returns a string.

24
00:01:37,950 --> 00:01:41,930
OK, and I'm going to give it a second required function.

25
00:01:41,940 --> 00:01:51,360
If you create a type animal, it must implement the says function and it must implement the number of

26
00:01:51,540 --> 00:01:55,980
legs function, which takes no arguments and returns in it there.

27
00:01:56,070 --> 00:01:58,170
I have just defined an interface.

28
00:01:58,200 --> 00:02:00,540
I haven't done anything with it, but I've defined it.

29
00:02:00,750 --> 00:02:10,230
All I'm saying is if you create a type that needs to satisfy the animal interface, it must have these

30
00:02:10,230 --> 00:02:11,030
two functions.

31
00:02:11,040 --> 00:02:12,320
So let's create a variable.

32
00:02:12,900 --> 00:02:19,440
I'll create another type that I'll call this one dog and it's going to be a struct, just the same way

33
00:02:19,440 --> 00:02:20,730
as we do the user type.

34
00:02:21,000 --> 00:02:26,580
And it only has one required field or one field in there and it will be a name of type string.

35
00:02:26,700 --> 00:02:30,090
So I'm going to store the name of the dog in that string, OK, whatever it may be.

36
00:02:31,470 --> 00:02:33,930
And let's put it in here as well.

37
00:02:33,930 --> 00:02:35,700
Breed string.

38
00:02:37,800 --> 00:02:46,860
So the dog type has a name and a breed and then I'm going to create another one type gorilla, which

39
00:02:46,860 --> 00:02:47,390
I better spell.

40
00:02:47,410 --> 00:02:47,610
Right.

41
00:02:49,470 --> 00:02:50,610
And it too is a struct.

42
00:02:51,180 --> 00:02:58,050
And similarly to the dog, it has a name as a field and then it also has color.

43
00:02:58,390 --> 00:03:02,580
I'm not sure what color gorillas gorillas are, but I'll pick one in a little while and it's going to

44
00:03:02,580 --> 00:03:03,990
be it's going to be a string.

45
00:03:04,260 --> 00:03:06,330
And it also has I need something else.

46
00:03:06,330 --> 00:03:07,080
Number of teeth.

47
00:03:08,910 --> 00:03:09,650
And that's neat.

48
00:03:09,960 --> 00:03:16,770
OK, so I've now defined one interface type and two struct types and now I have my main function.

49
00:03:16,770 --> 00:03:24,840
So what I'm going to do in here to start with is create a dog and that dog will be of type dog and I'll

50
00:03:24,840 --> 00:03:26,220
fill out it's necessary fields.

51
00:03:26,220 --> 00:03:36,180
So my dog has a name name and the name will be Samson and the breed will be German Shepherd.

52
00:03:37,860 --> 00:03:38,640
Shepherd.

53
00:03:39,480 --> 00:03:42,270
OK, so I now created this dog variable.

54
00:03:42,870 --> 00:03:49,580
So this dog variable is of type dog, but it does not yet satisfy the interface of type animal because

55
00:03:49,590 --> 00:03:50,910
I've not created these two functions.

56
00:03:50,910 --> 00:03:51,900
So let's do that right now.

57
00:03:53,730 --> 00:04:01,260
Underneath here I will create a function and I will call it give it a receiver type and the receiver

58
00:04:01,260 --> 00:04:02,400
type can be whatever I want.

59
00:04:02,400 --> 00:04:09,390
I'll call it D for dog, but it has to have the name says and it has to take no arguments and it has

60
00:04:09,390 --> 00:04:10,440
to return a string.

61
00:04:11,490 --> 00:04:14,160
And all I'm going to do is return Wolf.

62
00:04:15,660 --> 00:04:20,790
Then I'll create another function to satisfy the second requirement for the animals type it again will

63
00:04:20,790 --> 00:04:28,140
be of the receiver type dog which I've defined up here, and it has to have a name and the name is Number

64
00:04:28,170 --> 00:04:31,380
of Legs and it takes no arguments and it returns in it.

65
00:04:31,500 --> 00:04:33,480
So I'll return for.

66
00:04:34,860 --> 00:04:35,280
All right.

67
00:04:35,280 --> 00:04:38,520
So now I've created this dog variable.

68
00:04:38,910 --> 00:04:45,000
I've implemented the two things necessary to make it off type animal, but I still haven't used the

69
00:04:45,000 --> 00:04:45,660
animal type.

70
00:04:46,260 --> 00:04:53,940
So let's come down here and create another function func and I'll call this one Pritt info.

71
00:04:54,090 --> 00:05:00,060
OK, so print info and it takes an argument, a of type animal.

72
00:05:01,230 --> 00:05:06,870
And it doesn't return anything, it just prints information logged print line, this animal

73
00:05:10,440 --> 00:05:24,810
says, and then I'll call the method that's built into that type animal, a dart says, and has a number

74
00:05:24,810 --> 00:05:29,670
of legs, hips, legs.

75
00:05:30,360 --> 00:05:30,740
All right.

76
00:05:31,140 --> 00:05:32,310
So I've done this.

77
00:05:32,460 --> 00:05:37,340
Let's just pass this dog, which is of type dog, not type animal.

78
00:05:37,650 --> 00:05:42,240
Let's pass it to this function print info, which requires a type animal.

79
00:05:42,870 --> 00:05:52,680
So I can just say print info dog and look, no errors, no errors, even though this function is expecting

80
00:05:52,890 --> 00:06:00,990
it to get a type of animal and I'm passing it a type of dog that works and it works because I may dog

81
00:06:00,990 --> 00:06:08,070
my variable dog, satisfy the requirements for the interface by having both of the required methods

82
00:06:08,100 --> 00:06:09,480
which are specified up here.

83
00:06:09,900 --> 00:06:10,770
Now let's try this.

84
00:06:10,920 --> 00:06:19,320
Let's create a new variable called Gorilla, and it's a type gorilla and I'll fill out its fields.

85
00:06:19,320 --> 00:06:28,650
So Gorilla has a name name King Kong, and it has a color which I'll call black, which I'm not going

86
00:06:28,650 --> 00:06:33,050
to use, but I'm putting it in there just to show that it's a different kind of type and number of teeth.

87
00:06:33,060 --> 00:06:34,380
I have no idea how many they have.

88
00:06:34,380 --> 00:06:38,700
They're going to go with 32 because that's a good even number and you usually have an even number of

89
00:06:38,700 --> 00:06:39,030
teeth.

90
00:06:39,480 --> 00:06:41,010
So I've created this gorilla type.

91
00:06:41,010 --> 00:06:42,210
It's defined up here.

92
00:06:42,210 --> 00:06:49,110
And if I try to pass that, so if I try to pass it, you print info, print info and I pass it gorilla,

93
00:06:49,860 --> 00:06:56,070
it's getting an error and the error is cannot use gorilla type gorilla as type animal type does not

94
00:06:56,070 --> 00:07:01,500
implement animal as some methods are missing and then it links a list to fix that.

95
00:07:01,500 --> 00:07:08,760
I can come down here, copy both of these that are of type dog paste them and change them to type gorilla

96
00:07:09,750 --> 00:07:15,930
and type gorilla and gorillas have two legs and I have no idea what the gorilla says.

97
00:07:15,930 --> 00:07:26,250
So but now that I've implemented the necessary methods for the type animal on my variable gorilla or

98
00:07:26,250 --> 00:07:29,040
my type gorilla, now I can pass that.

99
00:07:29,220 --> 00:07:30,690
And if I actually run this.

100
00:07:30,690 --> 00:07:33,390
So let's go over to my terminal window.

101
00:07:35,060 --> 00:07:35,550
And run it.

102
00:07:35,600 --> 00:07:37,680
Go run, Mingo.

103
00:07:38,240 --> 00:07:42,180
And it should work, this animal says Wolf and has four legs.

104
00:07:42,230 --> 00:07:44,240
This animal says grunts and has two legs.

105
00:07:44,240 --> 00:07:46,960
And I say, I have an extra parentheses in my function probably.

106
00:07:46,960 --> 00:07:47,750
And there it is right there.

107
00:07:48,290 --> 00:07:49,790
But that's all there is to it.

108
00:07:49,970 --> 00:07:57,050
Now, the great thing about this is that I can now create more generic functions that accept a type,

109
00:07:57,050 --> 00:08:01,630
an interface type, rather than a specific type like dog or gorilla.

110
00:08:01,790 --> 00:08:05,910
I can just create one that accepts animal and that's all there is to interfaces.

111
00:08:06,220 --> 00:08:12,830
The key thing to remember is that once you define an interface, anything else can implement that interface

112
00:08:12,830 --> 00:08:15,860
just by implementing the required methods.

113
00:08:15,890 --> 00:08:16,880
That's all you have to do.

114
00:08:16,910 --> 00:08:17,630
There's nothing else.

115
00:08:17,630 --> 00:08:18,560
There's no implements.

116
00:08:18,560 --> 00:08:24,110
Keyword there's no explicitly declaring that it implements a particular interface.

117
00:08:24,110 --> 00:08:28,610
You just implement the required signature and you are good to go.

118
00:08:28,610 --> 00:08:32,240
And this will be incredibly useful to us as time goes on.
