1 00:00:00,000 --> 00:00:01,600 When talking about objects, 2 00:00:01,600 --> 00:00:04,544 there's typically two ways to think about equality. 3 00:00:04,544 --> 00:00:10,023 There's strict equality, which makes sure they're literally the same object. 4 00:00:10,023 --> 00:00:12,312 And then there's looks like equality, 5 00:00:12,312 --> 00:00:16,898 where as long as the two things have the same properties, then they are the same. 6 00:00:16,898 --> 00:00:18,380 Here's an example. 7 00:00:18,380 --> 00:00:22,305 Let's say we've got a playing card class with two fields, one for 8 00:00:22,305 --> 00:00:25,870 the number on the card and one for the card suit. 9 00:00:25,870 --> 00:00:30,340 Then let's create two new cards and make them both a two of clubs. 10 00:00:30,340 --> 00:00:33,600 By default, Java uses strict equality, 11 00:00:33,600 --> 00:00:38,620 meaning these two cards are not equal and would have different hash codes. 12 00:00:38,620 --> 00:00:42,110 But in some situations, you might want these to be equal. 13 00:00:42,110 --> 00:00:46,160 I mean, after all, they are exactly the same card. 14 00:00:46,160 --> 00:00:51,910 In this case, rather than basing equality on them literally being the same object, 15 00:00:51,910 --> 00:00:56,460 you'd want to base equality on the properties of that object instead, 16 00:00:56,460 --> 00:01:01,390 which can be accomplished by overriding the equals and hash code methods. 17 00:01:01,390 --> 00:01:04,410 Let's see how to do this with our animal class. 18 00:01:04,410 --> 00:01:06,910 But first, let's start in the main method and 19 00:01:06,910 --> 00:01:11,520 see what we get when we compare two dogs that are exactly the same. 20 00:01:11,520 --> 00:01:19,042 Let's delete the findFood line, And then rename this to dog1. 21 00:01:19,042 --> 00:01:22,931 Then let's add a dog2 on the next line. 22 00:01:22,931 --> 00:01:27,042 Dog dog2 = new Dog. 23 00:01:27,042 --> 00:01:30,795 And let's finish up by printing out whether they equal each other. 24 00:01:30,795 --> 00:01:38,052 So S out, dog1.equals(dog2). 25 00:01:38,052 --> 00:01:44,420 Then let's run it, And it looks like they're not equal. 26 00:01:44,420 --> 00:01:49,536 Awesome, now we need to change our animal class so that equality will be 27 00:01:49,536 --> 00:01:55,864 based only on the properties of the class, which, for us, is just the sound property. 28 00:01:55,864 --> 00:01:58,972 To do this, we'll need to override the equals and 29 00:01:58,972 --> 00:02:03,200 hash code methods to be based on the sound property. 30 00:02:03,200 --> 00:02:06,480 However, this typically isn't done by hand. 31 00:02:06,480 --> 00:02:08,210 So let's use the shortcut. 32 00:02:08,210 --> 00:02:14,458 Add some space at the bottom of the class, and then hit Command+N for 33 00:02:14,458 --> 00:02:18,840 Mac, or Alt+Insert for Windows to bring up the generate dialog. 34 00:02:18,840 --> 00:02:24,751 Then pick equals and hashCode, and just keep hitting Enter. 35 00:02:24,751 --> 00:02:28,217 Nice, let's run the code again. 36 00:02:30,630 --> 00:02:35,477 And now, since they have the same sound property, they're the same dog. 37 00:02:35,477 --> 00:02:38,542 So I guess that means if we ever want different dogs again, 38 00:02:38,542 --> 00:02:40,747 they'll need to make different sounds. 39 00:02:40,747 --> 00:02:43,627 How about instead of all dogs saying bark, 40 00:02:43,627 --> 00:02:46,917 we update the dog constructor to take in a sound. 41 00:02:49,957 --> 00:02:54,138 In the dog class, let's add in a sound parameter to the constructor. 42 00:02:54,138 --> 00:02:59,036 String sound, and let's update the call to super 43 00:02:59,036 --> 00:03:03,008 to just pass along our sound parameter. 44 00:03:03,008 --> 00:03:08,364 Then let's fix the errors in our main method 45 00:03:08,364 --> 00:03:13,734 by making dog1 say bark and dog2 say woof. 46 00:03:13,734 --> 00:03:20,500 And if we run it again, We're back to having different dogs. 47 00:03:21,725 --> 00:03:25,580 There's a ton you can do with objects and inheritance. 48 00:03:25,580 --> 00:03:30,050 In fact, I wouldn't be surprised if you start thinking of the world as a bunch of 49 00:03:30,050 --> 00:03:33,450 objects, with all sorts of different family trees. 50 00:03:33,450 --> 00:03:37,642 And for that, I apologize, but that's just the life of a developer. 51 00:03:37,642 --> 00:03:39,897 Until next time.