1
00:00:03,220 --> 00:00:09,254
In this short video, we're gonna take a
look at interfaces in Java. Interfaces

2
00:00:09,254 --> 00:00:14,040
specify relationships between classes
without using inheritance. So, here is an

3
00:00:14,040 --> 00:00:18,702
example: uh, we have an interface here
called the point interface. And a point

4
00:00:18,702 --> 00:00:23,488
interface can have a, a bunch of methods
in it, and, and we just declare the, the

5
00:00:23,488 --> 00:00:28,457
signature of those methods. You can also
have other things besides methods, but uh,

6
00:00:28,457 --> 00:00:33,292
the main thing that uh, they're used for
usually is for a, a method interface. So

7
00:00:33,292 --> 00:00:37,778
uh, here's an example of a particular
method, the move method, and it takes some

8
00:00:37,778 --> 00:00:42,148
arguments and has a particular return
type. Now any other class, or any class,

9
00:00:42,148 --> 00:00:46,749
excuse me, that's going to implement the
point interface has to provide a method

10
00:00:46,749 --> 00:00:51,407
uh, with the same signature. So, if this,
see because the point interface has a move

11
00:00:51,407 --> 00:00:55,918
method, the. A class will have to have a
move method with the same signature, as

12
00:00:55,918 --> 00:01:00,382
the move method in the declared interface.
And if the point interface had other

13
00:01:00,382 --> 00:01:04,666
methods ah, then the point class would
also have to implement those methods by,

14
00:01:04,666 --> 00:01:08,786
you know having a method of the same name
ah, with the appropriate types of

15
00:01:08,786 --> 00:01:14,463
arguments and result. Now it says in the
Java language manual that Java programs

16
00:01:14,463 --> 00:01:20,080
can use interfaces to make it unnecessary
for related classes to share a common

17
00:01:20,080 --> 00:01:25,768
abstract super class or to add methods to
objects. And the translation of that is

18
00:01:25,768 --> 00:01:31,175
that interfaces play the same role as
multiple inheritance in c plus, plus. So

19
00:01:31,175 --> 00:01:36,281
interfaces uh, are really analogous uh,
to, to multiple inheritance. And the

20
00:01:36,281 --> 00:01:41,430
reason for that, is that, a, a class can
implement multiple interfaces. So, if I

21
00:01:41,430 --> 00:01:46,629
have a class x and it implements a three
interfaces a, b, and c. This means an

22
00:01:46,629 --> 00:01:52,053
extra object can be treated as an A object
or B object or a C object in the

23
00:01:52,053 --> 00:01:57,620
appropriate context. So it's like or
almost as if X has three superclasses A, B

24
00:01:57,620 --> 00:02:03,115
and C. Now uh, there are some important
differences uh, but there is the effect,

25
00:02:03,115 --> 00:02:08,468
and so if I wanted to have a class that
gets functionality or implements a

26
00:02:08,468 --> 00:02:13,250
functionality uh, several uh, uh,
interfaces that's, I mean we do very

27
00:02:13,250 --> 00:02:18,674
directly in java just by saying if the
classes going to implement all those

28
00:02:18,674 --> 00:02:24,459
interfaces. >> Now here is an example of
an application of that, so think about a

29
00:02:24,459 --> 00:02:29,247
graduate student ah, working at Stanford
or some other university, so ah, typically

30
00:02:29,247 --> 00:02:33,918
graduate students are students, okay they
take classes and have property's that

31
00:02:33,918 --> 00:02:38,529
students ah, have they get degrees and
grades and things like that. >> Graduate

32
00:02:38,529 --> 00:02:43,199
students also work for the University? >>
They are often teaching assistants in

33
00:02:43,199 --> 00:02:48,224
classes or research assistant [inaudible]
so there [inaudible] another role which is

34
00:02:48,224 --> 00:02:52,658
university employee, and if I have gone to
ah, trouble ah, in my. I, university

35
00:02:52,658 --> 00:02:57,739
personnel management software to implement
functionality to deal with students and to

36
00:02:57,739 --> 00:03:02,348
implement functionality to deal with
employees. Well, then I would like to make

37
00:03:02,348 --> 00:03:07,193
use of that when I get around to thinking
about how I'm going to implement uh, the

38
00:03:07,193 --> 00:03:11,829
functionality for graduate students and
one way to do that. Would be if I had a

39
00:03:11,829 --> 00:03:16,483
class with implement, if I had, excuse me,
an interface for employees and interface

40
00:03:16,483 --> 00:03:21,252
for student, and I would say that graduate
student could be both, okay. So a graduate

41
00:03:21,252 --> 00:03:25,818
student can implement both the employee
interface and student interface. And, and

42
00:03:25,818 --> 00:03:30,206
the reason that's this is a good idea, is
it is actually hard to do this if ah, you

43
00:03:30,206 --> 00:03:34,701
only have single inheritance. If you think
about it, if I had set things up so that I

44
00:03:34,701 --> 00:03:39,143
had some employee classes and some student
classes and now I want to make graduate

45
00:03:39,143 --> 00:03:43,444
students. Well now what am I going to do?
Well if I have my employee class. I can

46
00:03:43,444 --> 00:03:49,030
make grad students a subclass of that but
now how do I get the student functionality

47
00:03:49,030 --> 00:03:54,267
and similarly. If I have a student class,
I can make graduate student a subclass of

48
00:03:54,267 --> 00:03:59,219
that. But now, how do I get the employee
functionality? So in single inheritance,

49
00:03:59,219 --> 00:04:04,044
you're forced to choose a single class to
inherit from. And the advantage of

50
00:04:04,044 --> 00:04:09,123
interfaces is that it will let you get
functionality or implement functionality,

51
00:04:09,123 --> 00:04:14,329
or express the relationship, at least, of
functionality to multiple kinds of things.

52
00:04:14,329 --> 00:04:18,963
And so I can have one, uh, graduate
student class that implements both the

53
00:04:18,963 --> 00:04:24,853
employee and the student functionality. So
how are interfaces different from

54
00:04:24,853 --> 00:04:29,665
inheritance? Well, probably the biggest
difference is that it's not possible to

55
00:04:29,665 --> 00:04:34,602
implement interfaces as efficiently as
inheritance. And that's why you have both.

56
00:04:34,602 --> 00:04:39,476
So you'd prefer to use inheritance, uh, if
you can, because it's going to be more

57
00:04:39,476 --> 00:04:44,166
efficient than interfaces. And what makes
interfaces less efficient? Well, the

58
00:04:44,166 --> 00:04:49,299
primary thing. Is that if the class is
implementing interfaces need not be at

59
00:04:49,299 --> 00:04:54,518
fixed offsets. In fact, we will not be
able, in general, uh, to assign methods in

60
00:04:54,518 --> 00:04:59,533
interfaces to fixed offsets inside of a
class implementation or an object

61
00:04:59,533 --> 00:05:04,430
implementation. So let's take a look at an
example. Here's our point interface again.

62
00:05:04,430 --> 00:05:08,524
Now say we have one class point when we
saw this one before [inaudible]. And it

63
00:05:08,524 --> 00:05:12,256
implements the move method has to
implement the move method. And then we

64
00:05:12,256 --> 00:05:16,298
have another class that also points to
point interface but it also implements

65
00:05:16,298 --> 00:05:21,159
some other stuff. Okay so, it might
implement some other methods that aren't

66
00:05:21,159 --> 00:05:26,717
part of that interface. So now how would
we decide you know, where to put the move

67
00:05:26,717 --> 00:05:32,108
method. Well the natural thing, that, that
we've discussed. [inaudible] Say of course

68
00:05:32,108 --> 00:05:37,111
[inaudible] is that the methods ah, would
be laid out in the order in which they are

69
00:05:37,111 --> 00:05:41,816
declared, so if we did that clearly the
move method will not be ah, in the first

70
00:05:41,816 --> 00:05:46,506
position in both of these classes. Now. We
could imagine, uh, a separate compiler

71
00:05:46,506 --> 00:05:50,995
pass, that we're trying to sort the
methods. So that, say, all the methods of

72
00:05:50,995 --> 00:05:56,211
the point interface always appeared in the
same position and in the same order in any

73
00:05:56,211 --> 00:06:01,125
class, implements the point interface. But
that doesn't work as soon as we have, um,

74
00:06:01,125 --> 00:06:06,038
multiple interfaces being implemented. So
let's say that the point two class here

75
00:06:06,038 --> 00:07:09,982
implemented another interface A. So, how
can we then implement interfaces? Well,

76
00:07:09,982 --> 00:07:15,605
so, it's going to be a little more complex
than usual to implement in this batch, say

77
00:07:15,605 --> 00:07:20,826
to a method f, where e has some interface
type. So if e is typed as having some

78
00:07:20,826 --> 00:07:26,383
interface and now we're calling it the f
method of that interface, then we're going

79
00:07:26,383 --> 00:07:31,282
to have to do a little bit more work. And
so here's one approach, this approach is

80
00:07:31,282 --> 00:07:35,796
actually quite inefficient, but you will
see that it will work. And there are other

81
00:07:35,796 --> 00:07:39,980
approaches that are more efficient, but
that's not particularly important, so

82
00:07:39,980 --> 00:07:44,329
here's one way that can work. So each
class of implements interface is going to

83
00:07:44,329 --> 00:07:48,733
have a look-up table associated with it
that maps method names, the string names

84
00:07:48,733 --> 00:07:53,314
of methods, to those methods themselves.
And then. Uh, we can hash the method names

85
00:07:53,314 --> 00:07:58,005
for faster look-up and we can actually
compute. Uh, those hashes at compile time.

86
00:07:58,005 --> 00:08:02,505
And so the idea would be that if when we
have an object. Ah, somewhere in the

87
00:08:02,505 --> 00:08:07,265
object, probably at the dispatch pointer.
The dispatch pointer you know will point

88
00:08:07,265 --> 00:08:11,907
off to a list of methods, sort of the
normal methods of the class. But somewhere

89
00:08:11,907 --> 00:08:16,960
say maybe at the end of the dispatch table
there will be another pointer to some kind

90
00:08:16,960 --> 00:08:22,083
of look up table that maps names. Two, two
methods to quote. Okay. So, somehow

91
00:08:22,083 --> 00:08:28,218
associated with every object of every
class ah, we will have this look up table

92
00:08:28,218 --> 00:08:34,119
that will map ah, the, the names of
interface methods to the actual codes for

93
00:08:34,119 --> 00:06:10,365
those methods that influence them. All
right and we'd already decided uh, that

94
00:06:10,365 --> 00:06:14,913
the, for the point interface that the move
method should go first. It should be the

95
00:06:14,913 --> 00:06:19,073
first method in the class. Well, if we had
made a similar decision for the a

96
00:06:19,073 --> 00:06:23,621
interface, some method in that interface
that should always be listed first in the

97
00:06:23,621 --> 00:06:28,400
class then we would have a conflict and in
general, there's no. Total ordering we can

98
00:06:28,400 --> 00:06:33,151
give to all the methods and all the
interfaces so that they can be maintained

99
00:06:33,151 --> 00:06:37,415
across all of the uh, classes that
implement those interfaces. At least

100
00:06:37,415 --> 00:06:42,167
there's no total order that we can give
without having to know in advance all

101
00:06:42,167 --> 00:06:46,918
classes that are declared and all the
interfaces that are declared. And that's

102
00:06:46,918 --> 00:06:51,609
kind of un-Java. And that we don't want to
uh, force people to declare all the

103
00:06:51,609 --> 00:06:57,058
classes and the interfaces once and not be
able to extend them in the future. Alright

104
00:06:57,058 --> 00:07:04,741
so the bottom line is that methods in
interfaces do not live at fixed process in
