1
00:00:01,310 --> 00:00:04,250
A class can override methods that it inherits.

2
00:00:07,410 --> 00:00:11,160
You saw that the object class is the common ancestor of every class.

3
00:00:13,070 --> 00:00:16,129
So every class inherits methods from the object class.

4
00:00:18,980 --> 00:00:22,730
Now, a class can customize, it can override methods that it inherits.

5
00:00:25,900 --> 00:00:28,960
So in this lesson, you're going to learn to override the equals method.

6
00:00:32,850 --> 00:00:38,940
When the double equal operator is used for objects, it compares the references, it returns true if

7
00:00:38,940 --> 00:00:40,080
the references are equal.

8
00:00:47,650 --> 00:00:50,890
So inside main job, a person to equal to person.

9
00:00:56,210 --> 00:00:59,660
And then check if person to double eagles person.

10
00:01:05,099 --> 00:01:05,990
Run the debugger.

11
00:01:09,890 --> 00:01:15,290
Person to any person share the same reference, so the double equals sign returns true.

12
00:01:16,280 --> 00:01:18,920
Now what if I said person two equal to a copy of person?

13
00:01:24,540 --> 00:01:25,560
We run the debugger.

14
00:01:28,590 --> 00:01:34,200
Person to store is a reference that points to a new object, and because they don't share the same reference,

15
00:01:34,410 --> 00:01:37,080
then the operator is going to determine that they're not equal.

16
00:01:40,330 --> 00:01:44,380
That being said, never used a double equals sign to compare objects.

17
00:01:50,280 --> 00:01:52,920
Remember, that object is the ancestor of every class.

18
00:01:53,070 --> 00:01:57,420
So by default, our person class is going to inherit this equals method.

19
00:01:59,900 --> 00:02:01,440
Let's use it and see what happens.

20
00:02:01,790 --> 00:02:04,730
We'll check if person two equals person one.

21
00:02:08,190 --> 00:02:09,060
Run the debugger.

22
00:02:16,890 --> 00:02:21,030
They don't share the same references, they do share the same fields.

23
00:02:23,590 --> 00:02:24,940
But it returns false.

24
00:02:34,590 --> 00:02:39,450
If you have another look at the equals method that we're inheriting, it returns true only if the references

25
00:02:39,450 --> 00:02:39,900
are equal.

26
00:02:42,880 --> 00:02:48,880
So hear us saying this is no different than saying this person two equals person one.

27
00:02:50,630 --> 00:02:53,000
You can even confirm this, relaunch the debugger.

28
00:03:01,390 --> 00:03:04,630
And it steps into the equals method inside the object class.

29
00:03:06,930 --> 00:03:12,010
And you can see that a default equals method that we're inheriting compares the references of two objects.

30
00:03:12,930 --> 00:03:15,240
That's why we need to override the equals method.

31
00:03:16,530 --> 00:03:22,410
Override means to change the logic of an inherited method, you're going to customize, you're going

32
00:03:22,410 --> 00:03:25,230
to override the equals method for the person class.

33
00:03:30,520 --> 00:03:32,380
You already know how to override an equals method.

34
00:03:32,410 --> 00:03:34,120
You've done it a million times before.

35
00:03:34,270 --> 00:03:38,380
All we have to do is replicate the signature of the equals method we're inheriting.

36
00:03:40,840 --> 00:03:45,520
And here we can write to whatever we want, we could print high and return false.

37
00:03:49,690 --> 00:03:55,250
But the point is, we overrode the equals method and JAV is now going to use this one instead, try

38
00:03:55,390 --> 00:03:59,980
it out, run the debugger and it calls the equals method we overrode.

39
00:04:08,650 --> 00:04:14,530
Now, there's a much easier way to override equals, just the right equals, and let Intellisense autocomplete

40
00:04:14,530 --> 00:04:16,930
the method for you and beautiful.

41
00:04:19,750 --> 00:04:21,880
Don't worry about what super is for now remove it.

42
00:04:25,820 --> 00:04:30,950
And now we can customize the equals method, just like we always do, if the object being passed in

43
00:04:30,950 --> 00:04:31,430
is No.

44
00:04:32,630 --> 00:04:33,560
Return false.

45
00:04:36,190 --> 00:04:39,580
If it's not an instance of the person class return false.

46
00:04:51,790 --> 00:04:54,280
If it is typecast, the object to person.

47
00:04:59,710 --> 00:05:04,060
And then compare the fields of the current object that's calling this method to the one that's being

48
00:05:04,060 --> 00:05:08,170
passed in return, the Stone Age equals person H.

49
00:05:10,960 --> 00:05:13,630
We're going to have to add some Gedda's first, let's do just that.

50
00:05:30,410 --> 00:05:31,130
And good.

51
00:05:34,510 --> 00:05:39,040
Now, the override annotation means we're overriding a method that we inherited.

52
00:05:39,460 --> 00:05:41,050
You don't have to include it.

53
00:05:41,080 --> 00:05:44,880
Remember, in the past, we never did, but you should leave it.

54
00:05:46,920 --> 00:05:52,050
Because if you misspell the method, it's going to throw an error and say there's no method equal to

55
00:05:52,050 --> 00:05:53,670
override, so that's good to have.

56
00:05:56,970 --> 00:06:01,260
Now, if you rerun the code, the person object calls the overwritten equals method.

57
00:06:17,520 --> 00:06:20,430
Let's recap, you overrode the equals method.

58
00:06:22,620 --> 00:06:28,980
Override it means changing the logic of an inherited method when you override a method, Jarvis starts

59
00:06:28,980 --> 00:06:30,180
using that one instead.

