1 00:00:00,300 --> 00:00:03,180 We can use the equals method to check if two objects are equal. 2 00:00:05,010 --> 00:00:07,710 In this lesson, you're going to add an equals method to a class. 3 00:00:10,090 --> 00:00:15,010 You can access this lesson by following this path and the resources and open the equals folder. 4 00:00:22,410 --> 00:00:26,400 The default equals method compares the references of two objects. 5 00:00:29,270 --> 00:00:31,250 Create one object of the car class. 6 00:00:35,710 --> 00:00:37,060 Toyota blew. 7 00:00:41,490 --> 00:00:44,460 And then set a variable, same car equal to the car. 8 00:00:49,600 --> 00:00:53,740 Now, both car and same car share a reference to the same object. 9 00:00:54,920 --> 00:00:58,010 Check if CA equals same car. 10 00:01:08,140 --> 00:01:11,980 And it returns true because both variables store the same reference. 11 00:01:19,720 --> 00:01:26,140 When you set same car equal to car, it copies the reference, and since both share the same reference, 12 00:01:26,320 --> 00:01:29,320 then the equals method determines that they are indeed equal. 13 00:01:34,080 --> 00:01:38,530 But that's the thing, too, variables should not share a reference to the same object. 14 00:01:38,580 --> 00:01:41,310 Otherwise, you're going to risk falling into the reference trap. 15 00:01:44,220 --> 00:01:48,210 So we're going to see that same car equal to a new copy of the first car object. 16 00:01:56,260 --> 00:02:02,650 The two objects are exact copies of each other, but because they don't share the same reference than 17 00:02:02,650 --> 00:02:05,170 the equals method determines that they're not equal. 18 00:02:07,430 --> 00:02:10,910 We can customize the equals method for a specific class. 19 00:02:12,040 --> 00:02:18,430 We can customize it to compare the fields of two objects, we're going to customize the equals method 20 00:02:18,430 --> 00:02:20,200 for objects of the car class. 21 00:02:38,410 --> 00:02:44,320 And as you can see, the equals method returns a boolean and it receives an object, what's the type 22 00:02:44,320 --> 00:02:45,070 of that object? 23 00:02:45,280 --> 00:02:46,230 We don't know. 24 00:02:46,270 --> 00:02:47,560 It can be any object. 25 00:02:55,940 --> 00:02:59,870 OK, car calls the equals method and pass the same cars in argument. 26 00:03:03,100 --> 00:03:06,340 This points to the current object calling the equals method. 27 00:03:07,710 --> 00:03:10,350 OBJ points to the object that was passed in. 28 00:03:12,940 --> 00:03:15,760 And nothing happens inside the function, it just returns true. 29 00:03:19,510 --> 00:03:20,730 That's not very useful. 30 00:03:23,210 --> 00:03:25,520 Here is how to make the perfect equals method. 31 00:03:27,350 --> 00:03:30,890 First, you're going to return false if the object that was passed then is null. 32 00:03:31,940 --> 00:03:36,890 Then you're going to return false if the object is not an instance of the specified type. 33 00:03:37,760 --> 00:03:42,160 If it happens to be an instance of the specified type, then you're going to typecast it. 34 00:03:43,010 --> 00:03:47,290 Then you're going to compare every field from both objects and return the results. 35 00:03:52,180 --> 00:03:56,140 In the equals method check if the object was passed in, is No. 36 00:04:01,910 --> 00:04:07,520 And if that's the case, return false, there's no way the object that's calling the equals method is 37 00:04:07,520 --> 00:04:10,820 going to equal null, otherwise it would have thrown a null pointer exception. 38 00:04:12,400 --> 00:04:18,279 All right, now return false if the object that was passed in is not an instance of car and we can do 39 00:04:18,279 --> 00:04:23,770 that using the instance of keyword, so we'll check if the object is an instance of car and reverse 40 00:04:23,770 --> 00:04:28,060 it to say if the object is not of type car, then we'll return false. 41 00:04:36,960 --> 00:04:42,240 Although if the object was passed in is an instance of car, then we are going to make it to this line 42 00:04:42,450 --> 00:04:45,450 so we can typecast object to a car type. 43 00:04:52,010 --> 00:04:53,990 And now we can access Hadfield's. 44 00:04:55,940 --> 00:05:01,040 So what I'm going to do is compare every field from the current object calling the equals method, this 45 00:05:01,250 --> 00:05:03,920 with every field from the car object that was passed in. 46 00:05:34,510 --> 00:05:38,710 OK, time to test it inside, men, I'm going to pass a nail into the equals method. 47 00:05:48,650 --> 00:05:54,500 This points to the current object and the argument we passed in is no to the equals method is going 48 00:05:54,500 --> 00:06:00,380 to return false because there's no way a null is going to equal the object that's calling this method. 49 00:06:05,730 --> 00:06:07,920 All right, now I'm going to create a scanner object. 50 00:06:16,640 --> 00:06:18,560 And pass it into the equals method. 51 00:06:31,740 --> 00:06:36,240 This points to the car object calling the method the object we passed in with scanner. 52 00:06:37,660 --> 00:06:40,510 The object is not no to passes this test. 53 00:06:42,500 --> 00:06:48,320 But the object is not an instance of car, so there's no way it's going to equal the car object that's 54 00:06:48,320 --> 00:06:53,960 calling this method equals returns false and determines that these objects are not equal. 55 00:07:03,010 --> 00:07:04,020 We'll try that out. 56 00:07:15,880 --> 00:07:18,580 The object is not know, so it passes this test. 57 00:07:22,520 --> 00:07:25,730 The object is of type car, so it passes that test. 58 00:07:31,550 --> 00:07:36,950 The fields from both objects are equal to each other, so this condition returns true and the equals 59 00:07:36,950 --> 00:07:38,810 method tells us that they are equal. 60 00:07:46,790 --> 00:07:51,260 OK, now we're going to compare two car objects that don't equal each other, so I'll change this to 61 00:07:51,260 --> 00:07:51,800 Honda. 62 00:08:05,430 --> 00:08:07,830 All right, this object is not known. 63 00:08:07,890 --> 00:08:09,300 So it passes this test. 64 00:08:11,190 --> 00:08:14,280 The object is of type car, so it passes this test. 65 00:08:17,040 --> 00:08:18,960 But the fields are equal to each other. 66 00:08:22,090 --> 00:08:27,600 So this condition returns false and the equals method determines that these objects are not equal, 67 00:08:28,330 --> 00:08:28,990 and you know what? 68 00:08:28,990 --> 00:08:30,790 If you ask me, it works perfectly. 69 00:08:33,520 --> 00:08:37,030 To recap, you customize the equals method for the car class. 70 00:08:39,669 --> 00:08:44,500 The default equals method compares the references of two objects, which isn't very useful. 71 00:08:46,200 --> 00:08:50,970 We customize the equals method to compare the fields from two car objects.