1 00:00:00,450 --> 00:00:02,910 A class can override methods that it inherits. 2 00:00:05,070 --> 00:00:10,650 Previously, you saw that the object class is the common ancestor of every class, so every class inherits 3 00:00:10,650 --> 00:00:12,390 methods from the object class. 4 00:00:15,040 --> 00:00:18,550 Now, clients can customize it can override methods that inherits. 5 00:00:21,220 --> 00:00:23,800 So in this lesson, you're going to override the two string method. 6 00:00:27,160 --> 00:00:33,480 Inside main Java, call to string from one of the objects and print the results again, we haven't defined 7 00:00:33,490 --> 00:00:35,490 a two string method inside the person class. 8 00:00:35,650 --> 00:00:37,540 So where is the two string coming from? 9 00:00:38,080 --> 00:00:42,020 Remember that the object class is the ancestor of every single class. 10 00:00:42,310 --> 00:00:46,390 So by default, our person class is going to inherit this two string method. 11 00:00:46,850 --> 00:00:48,510 So let's use it and see what happens. 12 00:00:51,930 --> 00:00:52,730 Run the debugger. 13 00:00:57,190 --> 00:01:02,410 And here it basically returns the identity of the object, the name of the class, followed by an ad 14 00:01:02,410 --> 00:01:06,550 symbol and he hexadecimal representation of the object has code. 15 00:01:11,360 --> 00:01:13,160 So back here, if I press next. 16 00:01:15,420 --> 00:01:20,630 It shows us what we expect the name of the class, followed by the add symbol and the objects code as 17 00:01:20,640 --> 00:01:21,930 a hexadecimal string. 18 00:01:22,910 --> 00:01:28,550 OK, now, instead of printing the hexadecimal, it would be nice if printing the object print its fields'. 19 00:01:30,750 --> 00:01:36,810 That's why we need to override the two string method override means to change the logic of an inherited 20 00:01:36,810 --> 00:01:37,230 method. 21 00:01:38,890 --> 00:01:44,020 You're going to customize you're going to override the two string method for the person class and you 22 00:01:44,020 --> 00:01:46,240 already know how to override a tutoring method. 23 00:01:46,270 --> 00:01:48,070 You've done it a million times before. 24 00:01:48,450 --> 00:01:52,210 All we have to do is replicate the signature of the two string method we're inheriting. 25 00:01:52,840 --> 00:01:54,450 And here we can write whatever we want. 26 00:01:54,520 --> 00:01:55,560 We could return high. 27 00:01:55,960 --> 00:02:01,030 But the point is we overrode the two string method and Java is going to use this one instead. 28 00:02:01,980 --> 00:02:03,960 So try it out, run the debugger. 29 00:02:08,039 --> 00:02:10,830 And it calls the tutoring method that we overrode. 30 00:02:12,250 --> 00:02:17,710 But there's a much easier way to override to string, just right to string and let Intellisense autocomplete 31 00:02:17,710 --> 00:02:22,150 the method for you and beautiful once again, don't worry about super will cover it later. 32 00:02:22,530 --> 00:02:25,830 And now we can just customise the two string method just like we always do. 33 00:02:26,230 --> 00:02:29,650 We'll just return the objects fields as part of a long string. 34 00:02:45,660 --> 00:02:50,500 And now remember, the override annotation just means we're overriding a method that we inherited. 35 00:02:50,880 --> 00:02:52,500 You don't have to include it. 36 00:02:52,500 --> 00:02:54,150 In the past, we never had it. 37 00:02:54,480 --> 00:02:59,220 But it's good practice to leave it because if you misspell the method, it's going to throw an error 38 00:02:59,220 --> 00:03:02,160 and say there's no method string whity to override. 39 00:03:02,460 --> 00:03:06,150 So just leaving it as good practice, it's going to prevent potential errors. 40 00:03:10,760 --> 00:03:15,500 Now, if we rerun the code to the person, object calls the overwritten it to string method. 41 00:03:19,340 --> 00:03:20,690 And it prints our fields. 42 00:03:26,320 --> 00:03:32,170 But recall, we don't need to explicitly call the tutoring method when you print an object java smart 43 00:03:32,170 --> 00:03:33,700 enough to internally call it. 44 00:03:42,360 --> 00:03:45,420 Let's recap, you overrode the two Tshering method. 45 00:03:47,660 --> 00:03:53,930 To override means to change the logic of an inherited method when you override a method, Java calls 46 00:03:53,930 --> 00:03:54,830 that one instead.