File size: 17,558 Bytes
407b94e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 | 1
00:00:05,000 --> 00:00:10,000
Hello, dear students, in this lesson, we are going to talk with you about interfaces from this lesson,
2
00:00:10,000 --> 00:00:13,000
you learn what interfaces are and what do they consist of.
3
00:00:13,000 --> 00:00:18,000
Also, we'll discuss what is the difference between abstract class and interface.
4
00:00:18,000 --> 00:00:23,000
And at the end of the lesson, I will tell you what is the difference between such terms as interface,
5
00:00:23,000 --> 00:00:24,000
class and time.
6
00:00:25,000 --> 00:00:30,000
Let's go as I just start and look at examples to understand what interfaces are.
7
00:00:30,000 --> 00:00:36,000
First of all, let's come up with a definition of an interface interfaces, an abstraction which defines
8
00:00:36,000 --> 00:00:41,000
a behavioral contract that must be followed by every class that implements this interface.
9
00:00:42,000 --> 00:00:48,000
In other words, you specify a set of expected behavior which should be present in each class that implements
10
00:00:48,000 --> 00:00:49,000
this interface.
11
00:00:49,000 --> 00:00:53,000
But it don't specify how exactly this behavior will be implemented.
12
00:00:53,000 --> 00:00:56,000
For example, have interface here for payment processor.
13
00:00:57,000 --> 00:01:03,000
I need to have some type in my program that will be able to process payment and send information about
14
00:01:03,000 --> 00:01:04,000
our payment to Payment Gateway.
15
00:01:05,000 --> 00:01:12,000
That's why I want to declare what method with the name process payment, the text payment data object
16
00:01:12,000 --> 00:01:13,000
as an argument.
17
00:01:13,000 --> 00:01:20,000
Payment data is simple project loss that has the payment amount tax amount idea of a user who wants
18
00:01:20,000 --> 00:01:22,000
to make a payment and currency.
19
00:01:23,000 --> 00:01:25,000
The rest of the message here I just getters and setters.
20
00:01:26,000 --> 00:01:27,000
Now let's get back to our interface.
21
00:01:28,000 --> 00:01:32,000
You see that I declared method without body and without any other modifiers.
22
00:01:33,000 --> 00:01:33,000
Why?
23
00:01:34,000 --> 00:01:39,000
Because invisibly, all matters in interface have modifiers, public and abstract.
24
00:01:39,000 --> 00:01:44,000
So in case I will add here public and abstract modifier, nothing will change.
25
00:01:45,000 --> 00:01:50,000
But there is no sense in any public abstract modifiers because all methods and interfaces by default
26
00:01:50,000 --> 00:01:52,000
are public and abstract.
27
00:01:52,000 --> 00:01:58,000
I can say that this interface said the contract on the process payment behavior by same contract.
28
00:01:58,000 --> 00:02:04,000
I mean, this interface clearly defines what a return type will be, how method will be called and what
29
00:02:04,000 --> 00:02:06,000
parameters will be in it, period.
30
00:02:07,000 --> 00:02:11,000
But this interface doesn't bother about what exact code will be written.
31
00:02:12,000 --> 00:02:17,000
And now when I have such interface in my program, I can create time that will implement this contract.
32
00:02:17,000 --> 00:02:23,000
I have changed payment processor and PayPal payment processor both implements payment processor interface.
33
00:02:24,000 --> 00:02:28,000
That means these classes are obligated to implement process payment methods.
34
00:02:29,000 --> 00:02:35,000
Since this method is abstract in the interface in our concrete class, we must implement this method.
35
00:02:35,000 --> 00:02:37,000
If only our class will be abstract.
36
00:02:38,000 --> 00:02:40,000
There is no need to implement this method.
37
00:02:40,000 --> 00:02:44,000
Let me call on this method and at absolute keyword before class.
38
00:02:44,000 --> 00:02:47,000
You see, there is no compilation error.
39
00:02:47,000 --> 00:02:52,000
But since this class is not abstract, there will be compilation error.
40
00:02:52,000 --> 00:02:58,000
In case we won't implement this method, you can press control plus one and select and implemented methods
41
00:02:58,000 --> 00:02:59,000
to generate stop method.
42
00:03:00,000 --> 00:03:02,000
I will just on existing one.
43
00:03:02,000 --> 00:03:04,000
This method will work differently.
44
00:03:04,000 --> 00:03:07,000
For example, chase payment processor.
45
00:03:07,000 --> 00:03:13,000
Because of the specifics of Payment Gateway, it should obtain security token first for authorization
46
00:03:14,000 --> 00:03:19,000
and only after that we can initialize payment page with secure token and the amount of money to pay.
47
00:03:20,000 --> 00:03:26,000
On the other hand, let's pretend that PayPal payment processor needs to request payment for by using
48
00:03:26,000 --> 00:03:27,000
a specific application key.
49
00:03:27,000 --> 00:03:32,000
And after that we have to fill out this form of payment details and submit it.
50
00:03:32,000 --> 00:03:36,000
You can notice that in both cases we have override annotation here.
51
00:03:36,000 --> 00:03:40,000
This notation exists for the sake of a successful compilation.
52
00:03:40,000 --> 00:03:41,000
Do not switch the topic.
53
00:03:41,000 --> 00:03:43,000
Let's keep focus on our interfaces.
54
00:03:43,000 --> 00:03:48,000
I will tell you more about overwrite annotation in our detailed lesson about polymorphism.
55
00:03:48,000 --> 00:03:50,000
Do we see these two examples?
56
00:03:51,000 --> 00:03:55,000
The behavior is the same both classes, protests, payment data.
57
00:03:55,000 --> 00:03:57,000
But they do this in different ways.
58
00:03:57,000 --> 00:04:00,000
What do all this look like exactly?
59
00:04:00,000 --> 00:04:06,000
This look like implementation of polymorphism principle, one of the main or P principle?
60
00:04:06,000 --> 00:04:07,000
Why else?
61
00:04:07,000 --> 00:04:12,000
We need interfaces to build our application was dependency on the obstruction types, but not on the
62
00:04:12,000 --> 00:04:13,000
concrete classes.
63
00:04:14,000 --> 00:04:15,000
Let me show you this example.
64
00:04:16,000 --> 00:04:18,000
You mentioned we have some client of our code.
65
00:04:18,000 --> 00:04:22,000
It could be some other class so-called facade class or controller class.
66
00:04:22,000 --> 00:04:23,000
That doesn't matter.
67
00:04:24,000 --> 00:04:28,000
And this class can simply rely on the interface of payment processor type.
68
00:04:29,000 --> 00:04:33,000
We can say that by default, variable of payment processor reference to PayPal payment processor.
69
00:04:34,000 --> 00:04:38,000
But in fact, this client doesn't bother with implementation of payment processor.
70
00:04:38,000 --> 00:04:42,000
We will use PayPal payment processor or chase payment processor.
71
00:04:42,000 --> 00:04:48,000
Moreover, somebody can use Setar and that is a chase or PayPal payment process during the runtime.
72
00:04:49,000 --> 00:04:55,000
The good thing about interfaces and they define level of abstraction and we can build our program relying
73
00:04:55,000 --> 00:04:56,000
on this abstraction.
74
00:04:56,000 --> 00:05:01,000
They see this method and location and checkout method no matter what implementation of payment.
75
00:05:01,000 --> 00:05:08,000
Sir, I will have here during Zeron time, I don't need to change this code and also pay attention to
76
00:05:08,000 --> 00:05:09,000
one more thing here.
77
00:05:09,000 --> 00:05:16,000
I can initialize variable of interface type with a reference to an object of any other class which implements
78
00:05:16,000 --> 00:05:17,000
this interface.
79
00:05:18,000 --> 00:05:20,000
OK, Holmes, this is clear.
80
00:05:20,000 --> 00:05:23,000
Now, let's take a look what our interface can contain.
81
00:05:23,000 --> 00:05:27,000
As you already understand, our interface can contain abstract methods.
82
00:05:27,000 --> 00:05:29,000
It can have as many methods as you wish.
83
00:05:30,000 --> 00:05:34,000
But later we will learn other object oriented principles and you will understand that it is better to
84
00:05:34,000 --> 00:05:37,000
have a lot of small interfaces rather than one.
85
00:05:37,000 --> 00:05:42,000
And besides abstract methods, our interface can contain default methods.
86
00:05:42,000 --> 00:05:48,000
Default methods are methods that have body so you can treat them like regular methods in concrete glass.
87
00:05:49,000 --> 00:05:54,000
But the Java syntax wouldn't allow you to declare a method with body and interface without default.
88
00:05:54,000 --> 00:06:02,000
Keyword in case iRace the default keyword compiler tells me that abstract methods don't have money because
89
00:06:02,000 --> 00:06:04,000
compiler treats this method as abstract.
90
00:06:05,000 --> 00:06:07,000
Also, our interface may contain static methods.
91
00:06:07,000 --> 00:06:12,000
We are going to have a separate lesson about static keywords, but right now you can remember the static
92
00:06:12,000 --> 00:06:15,000
methods can be called without any object.
93
00:06:15,000 --> 00:06:19,000
So you don't need to write newcomer's and create objects.
94
00:06:19,000 --> 00:06:23,000
You can just write type name and column method like I did here.
95
00:06:23,000 --> 00:06:28,000
For example, I wrote the name of the interface and invoke static method on this interface.
96
00:06:29,000 --> 00:06:32,000
Notice I didn't use variable of payment processor type.
97
00:06:32,000 --> 00:06:33,000
I use interface.
98
00:06:34,000 --> 00:06:36,000
It is not very object oriented approach.
99
00:06:36,000 --> 00:06:40,000
Why wait for the in the most cited keywords and I will tell you more about it.
100
00:06:41,000 --> 00:06:45,000
So far, you know that interface can contain abstract default and static methods.
101
00:06:45,000 --> 00:06:48,000
What else interface can have constants.
102
00:06:49,000 --> 00:06:53,000
For example, imagine the real life scenario in case our payment failed.
103
00:06:53,000 --> 00:06:55,000
We need to retrieve a few more times.
104
00:06:55,000 --> 00:07:02,000
I create variable of type retry attempts and you probably noticed that the clips for months, this variable
105
00:07:02,000 --> 00:07:06,000
name like a constant I'm in cursive and another color.
106
00:07:06,000 --> 00:07:10,000
That is because by default, all fields and interfaces have public static.
107
00:07:10,000 --> 00:07:17,000
Final modifiers like all constants have public so that everyone could access it static because constant
108
00:07:17,000 --> 00:07:22,000
should exist in a single copy and final so that nobody could override it.
109
00:07:22,000 --> 00:07:25,000
We will talk more about constants in the lesson about static keywords.
110
00:07:26,000 --> 00:07:30,000
And even if I would add these modifiers here, nothing will change.
111
00:07:30,000 --> 00:07:33,000
But usually developers give this modifiers in interfaces.
112
00:07:34,000 --> 00:07:37,000
Let me also removed important things that we didn't discuss yet.
113
00:07:37,000 --> 00:07:43,000
If the fact that class can implement multiple interfaces at once, I'm not sure whether you notice that
114
00:07:43,000 --> 00:07:49,000
our chase payment processor implements two interfaces, the one we discussed already, and another one
115
00:07:49,000 --> 00:07:51,000
is payment for the data.
116
00:07:51,000 --> 00:07:56,000
If this class implements payment for the data interface, that means this class should implement validate
117
00:07:56,000 --> 00:08:02,000
payment methods which will validate payment to be sure that it has all necessary information for payment
118
00:08:02,000 --> 00:08:03,000
processing.
119
00:08:03,000 --> 00:08:07,000
In this payment gateway in Java, it is allowed to implement multiple interfaces.
120
00:08:08,000 --> 00:08:11,000
But in one specific case, this can lead to compilation error.
121
00:08:11,000 --> 00:08:13,000
And I want you to know this case.
122
00:08:14,000 --> 00:08:15,000
Let's discuss it.
123
00:08:15,000 --> 00:08:21,000
For example, in payment processor interface, you have some default method and in payment for the data,
124
00:08:21,000 --> 00:08:24,000
you also have default method with the same signature.
125
00:08:24,000 --> 00:08:30,000
Here it this this can lead to compilation error in chase payment processor class, because during the
126
00:08:30,000 --> 00:08:33,000
runtime it will be unclear which default method should be used.
127
00:08:34,000 --> 00:08:37,000
The one from payment for the data was the one from payment processor.
128
00:08:38,000 --> 00:08:44,000
That's why it doesn't allow us to compile this code until we resolve issue was duplicated default methods.
129
00:08:44,000 --> 00:08:47,000
So now you know really a lot about interfaces.
130
00:08:48,000 --> 00:08:53,000
It is time to understand how good you learned interfaces and ask you what is the difference between
131
00:08:53,000 --> 00:08:55,000
abstract classes and interfaces.
132
00:08:55,000 --> 00:08:59,000
Absolute class we can extend with extensive keyboard interface.
133
00:08:59,000 --> 00:09:02,000
We can implement using implements, keyboard apps.
134
00:09:03,000 --> 00:09:06,000
A class can have properties with all possible modifiers.
135
00:09:06,000 --> 00:09:11,000
On the other hand, all properties interfaces have public static final modifiers.
136
00:09:12,000 --> 00:09:17,000
You can extend only one class in Java, but you can implement multiple interfaces.
137
00:09:17,000 --> 00:09:23,000
You remember that example when our PayPal payment processor implemented two interfaces at a time?
138
00:09:23,000 --> 00:09:27,000
Basically that the most significant difference between abstract classes and interfaces.
139
00:09:28,000 --> 00:09:33,000
It is also worth to mention that the introduction of default and static methods and interfaces made
140
00:09:33,000 --> 00:09:36,000
it harder to distinguish interfaces from abstract classes.
141
00:09:37,000 --> 00:09:42,000
Based on the difference which we discussed, you can select whether you need to use abstract class or
142
00:09:42,000 --> 00:09:43,000
interface.
143
00:09:43,000 --> 00:09:48,000
For example, if you know that you need to list properties with private or protected access modifiers
144
00:09:48,000 --> 00:09:53,000
that are not static, you already know that you need to use abstract class instead of interface.
145
00:09:53,000 --> 00:09:59,000
And if you need to be able to implement specific interface together with other ones, you know that
146
00:09:59,000 --> 00:10:00,000
you need to use interfaces.
147
00:10:01,000 --> 00:10:06,000
Last but not the least question for today and our agenda is to define the difference between such terms
148
00:10:06,000 --> 00:10:11,000
as class type and interface, you already know what a class is.
149
00:10:11,000 --> 00:10:13,000
Ensure that class is a template for your future.
150
00:10:13,000 --> 00:10:19,000
Object's interface is an abstraction which defines application programming interface and contract for
151
00:10:19,000 --> 00:10:20,000
behavior.
152
00:10:20,000 --> 00:10:27,000
But what is a type probably you heard during the lessons that I use type word as a synonym when I reference
153
00:10:27,000 --> 00:10:33,000
to class interface because class is a type interface is also a type.
154
00:10:33,000 --> 00:10:39,000
That means every type is also is a class and interface and enumeration or an array.
155
00:10:39,000 --> 00:10:43,000
Also, each object has a time and every expression has a type.
156
00:10:43,000 --> 00:10:50,000
If during that interview you will be asked what a typist you can answer something like this type summarizes
157
00:10:50,000 --> 00:10:53,000
the common features of a set of objects with the same characteristics.
158
00:10:54,000 --> 00:11:00,000
Class is a concrete data structure and group of methods that represents an implementation of the type.
159
00:11:01,000 --> 00:11:04,000
Let's look at this example one more time attentively.
160
00:11:04,000 --> 00:11:10,000
Payment processor variable has payment processor type, but this variable reference to the PayPal payment
161
00:11:10,000 --> 00:11:12,000
processor object.
162
00:11:12,000 --> 00:11:15,000
So we can say that the object has a payment processor type.
163
00:11:15,000 --> 00:11:22,000
And you see here in the method parameters, this sector can accept any object of type payment processor.
164
00:11:22,000 --> 00:11:29,000
That means this method can work with any object that implements this interface doesn't make sense.
165
00:11:29,000 --> 00:11:32,000
Hope with this examples, things become clearer.
166
00:11:33,000 --> 00:11:35,000
Let's recap what we have learned today.
167
00:11:35,000 --> 00:11:42,000
In this lesson, we learned what interfaces are, what interfaces can contain inside, how to implement
168
00:11:42,000 --> 00:11:48,000
interface, what default methods and interfaces are, what static methods interfaces are.
169
00:11:48,000 --> 00:11:52,000
Now, you know what the difference between abstract class and interface is.
170
00:11:53,000 --> 00:11:56,000
And you also learn the difference between type and object.
171
00:11:57,000 --> 00:11:58,000
That's all what we have for today.
172
00:11:59,000 --> 00:12:02,000
Thanks a lot for your attention and see you in the next lesson.
|