1 00:00:00,000 --> 00:00:01,365 Hopefully by this point, 2 00:00:01,365 --> 00:00:04,598 it's starting to click that everything in Java is an object. 3 00:00:04,598 --> 00:00:08,308 But what sort of functionality do we get from the object class? 4 00:00:08,308 --> 00:00:11,934 Up in the main method, let's clear out what we've got and 5 00:00:11,934 --> 00:00:14,236 start fresh with a new dog variable. 6 00:00:14,236 --> 00:00:18,670 Dog, name it dog = new Dog. 7 00:00:18,670 --> 00:00:22,895 Then let's add a line and type dog., to look at our options. 8 00:00:22,895 --> 00:00:27,615 Here, everything except for the sound field, and 9 00:00:27,615 --> 00:00:32,352 the makeSound method comes from the object class. 10 00:00:32,352 --> 00:00:36,308 Now, most of these you don't need to worry about, but 11 00:00:36,308 --> 00:00:42,854 there's a few you really should know like equals, hashCode, toString, and getClass. 12 00:00:42,854 --> 00:00:45,283 Let's start with the getClass method. 13 00:00:45,283 --> 00:00:48,694 When you call the getClass method on an object, 14 00:00:48,694 --> 00:00:54,253 it returns a class object that contains information about the class itself. 15 00:00:54,253 --> 00:00:57,599 Like the class name and what package it's in. 16 00:00:57,599 --> 00:01:02,457 Moving to the toString method, when you call toString on an object, 17 00:01:02,457 --> 00:01:06,408 it returns a string with information about the object. 18 00:01:06,408 --> 00:01:09,344 So if we wanted to print out what was in an object, 19 00:01:09,344 --> 00:01:11,431 we would use the toString method. 20 00:01:11,431 --> 00:01:13,585 Hashcode is a little different. 21 00:01:13,585 --> 00:01:16,569 When you call the hashcode method on an object, 22 00:01:16,569 --> 00:01:19,788 you get back an integer representing that object. 23 00:01:19,788 --> 00:01:23,657 However, the important thing is that the hashcode method will 24 00:01:23,657 --> 00:01:26,514 return a different integer for every object. 25 00:01:26,514 --> 00:01:30,947 So one way to check if an object is equal to another would be to check 26 00:01:30,947 --> 00:01:32,249 their hashcodes. 27 00:01:32,249 --> 00:01:35,718 If they've got the same hash code, they're the same object. 28 00:01:35,718 --> 00:01:40,363 The other way to check if two objects are equal would be to use the equals method. 29 00:01:40,363 --> 00:01:44,449 Which returns a boolean indicating if the two objects are equal. 30 00:01:44,449 --> 00:01:45,602 Awesome! 31 00:01:45,602 --> 00:01:49,117 Back in the code, lets get some practice with these methods. 32 00:01:49,117 --> 00:01:54,234 First let's print out the result of calling toString on our Dog object. 33 00:01:54,234 --> 00:01:59,604 Let's delete this dog., and then type sout, 34 00:01:59,604 --> 00:02:03,102 and print out dog.toString. 35 00:02:03,102 --> 00:02:08,027 And then, let's run the app, and there we go. 36 00:02:08,027 --> 00:02:11,266 We've got some information about our object. 37 00:02:11,266 --> 00:02:16,251 Looks like we've got the class name followed by an at, and 38 00:02:16,251 --> 00:02:19,010 then some letters and numbers. 39 00:02:19,010 --> 00:02:24,412 Let's take a deeper look at the toString method to figure out what's going on here. 40 00:02:24,412 --> 00:02:29,045 In IntelliJ, whenever you want to take a deeper look at something, 41 00:02:29,045 --> 00:02:35,370 you just put your cursor on it and use Cmd or Ctrl+B to jump to its declaration. 42 00:02:35,370 --> 00:02:42,760 Let's click on toString and then hit Cmd or Ctrl+V, and I'll hide the run pane. 43 00:02:42,760 --> 00:02:46,918 Here we have the toString method from the Object class. 44 00:02:46,918 --> 00:02:51,726 And if we look inside, we can see that it's returning get class 45 00:02:51,726 --> 00:02:56,186 dot get name to get the class name, followed by an at sign. 46 00:02:56,186 --> 00:03:01,184 And then it looks like that last bit was just the hash code represented as 47 00:03:01,184 --> 00:03:02,195 a hex string. 48 00:03:02,195 --> 00:03:05,138 Also, sometimes when you use Cmd or Ctrl+B, 49 00:03:05,138 --> 00:03:09,525 if you're lucky, you'll get some documentation like we did here. 50 00:03:12,387 --> 00:03:16,120 We already know most of this just from reading the code. 51 00:03:16,120 --> 00:03:17,530 But let's take a look at this bit. 52 00:03:18,620 --> 00:03:22,880 It is recommended that all subclasses override this method. 53 00:03:24,190 --> 00:03:25,370 Override? 54 00:03:25,370 --> 00:03:25,980 What does that mean? 55 00:03:27,010 --> 00:03:28,800 We'll find out in the next video.