Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,530 --> 00:00:03,620
When you add in equals method, you must always add the hash code method.
2
00:00:05,740 --> 00:00:10,000
In this lesson, you will add an equals method, any hash code method to the children classes.
3
00:00:11,610 --> 00:00:16,020
We're not going to add an equals method or a hash code method to the parent class, it's impossible
4
00:00:16,020 --> 00:00:17,480
to create an object of the product.
5
00:00:17,550 --> 00:00:23,370
There's nothing that can be equal to Serenity's the Java code generators that an equals and hash code
6
00:00:23,370 --> 00:00:24,510
method to the short class.
7
00:00:26,790 --> 00:00:29,070
Auto import, the objects, utility costs.
8
00:00:34,510 --> 00:00:40,660
And this equals isn't complete, it has to compare every field from both objects, but it's only comparing
9
00:00:40,660 --> 00:00:42,970
the size field and I find this a bit awkward.
10
00:00:42,970 --> 00:00:46,990
I'd rather just say this size equals shirt that size.
11
00:00:48,830 --> 00:00:54,920
OK, how do we compare the inherited fields, remember, that super refers to the super class, which
12
00:00:54,920 --> 00:00:56,570
also means the parent class.
13
00:01:00,050 --> 00:01:03,770
Superdog, a method calls a method from the parent class.
14
00:01:06,820 --> 00:01:09,280
So we'll check if Superdog get price.
15
00:01:13,460 --> 00:01:15,050
Equal share target price.
16
00:01:21,430 --> 00:01:22,660
Superdog, get color.
17
00:01:25,750 --> 00:01:27,370
Equal shirt, get color.
18
00:01:33,100 --> 00:01:34,630
And Superjet brand.
19
00:01:39,540 --> 00:01:41,100
Equal shirt, douget brand.
20
00:01:45,420 --> 00:01:51,780
Before we continue, super refers to the parent class superdog, get price calls, the get price method
21
00:01:51,780 --> 00:01:57,360
from inside the parent class and get price is going to return the price value of the current object
22
00:01:57,360 --> 00:01:58,530
that's calling this method.
23
00:02:00,580 --> 00:02:06,580
OK, now here, make sure this has a hash, not hash code, if the generator only sees one field that
24
00:02:06,580 --> 00:02:12,790
defaults, the calling the objects that hash code method, but we need to call it hash because it accepts
25
00:02:12,790 --> 00:02:13,920
many arguments.
26
00:02:13,930 --> 00:02:19,570
And here we have to add the remaining fields using the getas superdog set price superdog, that color.
27
00:02:23,250 --> 00:02:24,630
And Superdog get brand.
28
00:02:26,990 --> 00:02:30,580
The passcode good method needs to always stay consistent with your equals method.
29
00:02:33,620 --> 00:02:36,260
All right, now I can check if shirt and shirt to our equal.
30
00:02:42,880 --> 00:02:43,960
So run the debugger.
31
00:02:51,390 --> 00:02:56,910
This points to the current object that's calling this method of points to the object that was passed
32
00:02:56,910 --> 00:03:00,870
into the equals method, the two objects don't share the same reference.
33
00:03:00,880 --> 00:03:01,800
So this is suspect.
34
00:03:02,220 --> 00:03:05,300
The object being passed in is an instance of the Sherpas.
35
00:03:05,320 --> 00:03:10,290
So it passes this test and you can typecast it and now we can compare the size.
36
00:03:12,020 --> 00:03:18,260
And here super refers to the parent class superdog, get price calls, the get priced method from inside
37
00:03:18,260 --> 00:03:19,160
the parent class.
38
00:03:22,960 --> 00:03:26,440
And get price is going to return the price value of the current object.
39
00:03:30,450 --> 00:03:35,580
The price of that object is the same as the one we're comparing it to, the color is the same, the
40
00:03:35,580 --> 00:03:36,550
brand is the same.
41
00:03:36,720 --> 00:03:38,460
So ultimately equals returns.
42
00:03:38,460 --> 00:03:40,470
True, these objects are equal.
43
00:03:43,670 --> 00:03:46,340
All right, and now I'm going to print the hash good of both objects.
44
00:04:02,720 --> 00:04:09,440
Endure the same Perfect's passcode method must always assign equal objects, the same hash code, this
45
00:04:09,440 --> 00:04:14,360
rule is so important and if you don't follow it, you're going to get bugs on that very same topic.
46
00:04:14,390 --> 00:04:19,190
I want to remind you once again, because this is so important, when you override an equals method,
47
00:04:19,190 --> 00:04:21,980
you must always, always override the hash code method.
48
00:04:22,350 --> 00:04:26,750
If you don't do that, you're breaking the equality contract and you're going to get so many bugs.
49
00:04:28,410 --> 00:04:29,430
Anyway, so we're good.
50
00:04:29,880 --> 00:04:33,480
I want to talk about one more thing before we wrap up this versus super.
51
00:04:36,450 --> 00:04:40,110
This can call local methods and inherited methods.
52
00:04:42,390 --> 00:04:45,060
Super can only call inherited methods.
53
00:04:47,680 --> 00:04:52,360
So going back to our code, let me ask you this, should we use this or super.
54
00:04:53,870 --> 00:04:57,590
Super, we don't want the added flexibility because it's dangerous.
55
00:04:58,000 --> 00:05:01,180
This is perfectly capable of calling an inherited method.
56
00:05:01,670 --> 00:05:03,380
In fact, rerun the debugger.
57
00:05:11,660 --> 00:05:15,110
This points to the current object that's calling the equals method.
58
00:05:16,720 --> 00:05:23,220
The current object inherits a get price method, so it's capable of calling it price get price returns,
59
00:05:23,230 --> 00:05:24,880
the price of the current object.
60
00:05:27,940 --> 00:05:28,480
OK.
61
00:05:32,100 --> 00:05:36,810
But now what if in the future I end up creating a random method that happens to be called get price
62
00:05:36,810 --> 00:05:38,040
inside the shirtless?
63
00:05:40,220 --> 00:05:42,020
And we'll just return three.
64
00:05:46,010 --> 00:05:47,270
Now restart the runtime.
65
00:05:54,550 --> 00:05:57,430
This is going to prioritize the local method.
66
00:05:59,110 --> 00:06:04,990
Even though we wanted to call the inherited method, and that's why we don't want to use this, it's
67
00:06:04,990 --> 00:06:07,690
more flexible, which also makes it more dangerous.
68
00:06:07,700 --> 00:06:10,930
And in my opinion, a good code should protect you from yourself.
69
00:06:13,340 --> 00:06:18,360
So if you put super, the current object is going to call the inherited method no matter what.
70
00:06:18,620 --> 00:06:19,850
So restart the runtime.
71
00:06:24,110 --> 00:06:30,380
Super refers to the super class, the parent class, so super doget price is going to call the get price
72
00:06:30,380 --> 00:06:33,470
method inside the parent class no matter what.
73
00:06:34,160 --> 00:06:38,000
Now, that being said, I always use super to call an inherited method.
74
00:06:39,720 --> 00:06:43,050
But this year can still cause problems, so just make sure to remove it.
75
00:06:47,050 --> 00:06:51,790
All right, now we'll do the same thing inside the pants class, use the Java code generator stat equals
76
00:06:51,790 --> 00:06:53,290
method, any hash code method.
77
00:07:00,070 --> 00:07:04,960
We need to compared the other three fields, so we'll check if superguy price.
78
00:07:10,400 --> 00:07:12,350
Equals penstock get price.
79
00:07:14,800 --> 00:07:16,150
If superdog that color.
80
00:07:18,070 --> 00:07:19,660
Equals pants that get color.
81
00:07:22,970 --> 00:07:24,650
And if Superjet brand.
82
00:07:28,020 --> 00:07:30,030
Equals Penstock get brand.
83
00:07:31,770 --> 00:07:37,320
OK, now here, make sure this has hash, not hash code, because hash can accept many arguments and
84
00:07:37,320 --> 00:07:38,550
we need to pass in three of them.
85
00:07:38,550 --> 00:07:41,220
Superdog get price, superdog at color.
86
00:07:42,750 --> 00:07:44,130
And supermarket brand.
87
00:07:52,660 --> 00:07:56,530
I'm not going to test this code, you can feel free to do so on your own time, it should behave the
88
00:07:56,530 --> 00:07:58,480
same as what we did for the short class.
89
00:08:00,720 --> 00:08:05,490
Let's recap, you created the equals and hash code method inside the children classes.
90
00:08:07,080 --> 00:08:10,590
And Superdog Method calls a method inside the parent class.
8678
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.