1 00:00:00,460 --> 00:00:04,010 The first annotation we're going to look at is called override. 2 00:00:04,010 --> 00:00:07,470 Now, you can't get too far in Java without running into this one. 3 00:00:08,620 --> 00:00:13,510 The override annotation is processed and discarded when your code is compiled. 4 00:00:13,510 --> 00:00:16,770 Essentially what it does, is send a message to the Java compiler, 5 00:00:16,770 --> 00:00:20,610 asking hey, when you compile this code can you tell me whether or 6 00:00:20,610 --> 00:00:25,280 not this method I've annotated properly overrides a method. 7 00:00:25,280 --> 00:00:29,380 If the answer is no, then the compiler will produce an error. 8 00:00:29,380 --> 00:00:31,870 It is good practice to use this annotation 9 00:00:31,870 --> 00:00:36,630 because it allows you to use the compiler to verify proper inheritance. 10 00:00:36,630 --> 00:00:39,890 One of the most common examples of using the override annotation 11 00:00:39,890 --> 00:00:41,200 would be a toString() method. 12 00:00:42,350 --> 00:00:45,110 Let's experiment with writing with one of these, and see how we can 13 00:00:45,110 --> 00:00:49,390 us the override annotation to ensure that we are properly overriding a method. 14 00:00:51,180 --> 00:00:54,860 In this workspace I have a simple Java project with a few packages. 15 00:00:54,860 --> 00:00:58,480 You can find a link to the project in the teacher notes if you'd like to download it 16 00:00:58,480 --> 00:01:02,400 and import it to your favorite IDE, such as Intel JRE Eclipse. 17 00:01:02,400 --> 00:01:04,395 For this course, I'll stick with workspaces. 18 00:01:05,530 --> 00:01:08,117 To demonstrate the use of the override annotation, 19 00:01:08,117 --> 00:01:10,719 we'll make a simple class in the override package. 20 00:01:10,719 --> 00:01:14,565 How about we call it, cheese? 21 00:01:14,565 --> 00:01:16,300 We'll create a new file. 22 00:01:18,558 --> 00:01:21,010 Name it Cheese.Java. 23 00:01:22,430 --> 00:01:26,680 And we'll set up the structure of our class here, public class Cheese. 24 00:01:29,200 --> 00:01:31,638 Let's not forget our package. 25 00:01:39,260 --> 00:01:43,900 Brilliant, now we won't go into too much detail crafting the perfect cheese. 26 00:01:43,900 --> 00:01:45,800 There are many fine cheeses out there, so 27 00:01:45,800 --> 00:01:48,800 we don't need to go reinventing the wheel, get it? 28 00:01:48,800 --> 00:01:51,730 Wheel, like a cheese wheel? 29 00:01:51,730 --> 00:01:52,730 Oh, nevermind. 30 00:01:54,070 --> 00:01:57,410 Anyway, one of the most common tasks we wanna perform with an object 31 00:01:57,410 --> 00:02:00,310 is to display a string summary of the object. 32 00:02:00,310 --> 00:02:03,920 You've likely learned that this is accomplished via the toString() method. 33 00:02:03,920 --> 00:02:06,710 Let's write a simple one that returns a hard coded string. 34 00:02:07,840 --> 00:02:12,870 We'll start with out signature, public string toString(). 35 00:02:12,870 --> 00:02:17,780 And we will return a simple string that says, 'String cheese'. 36 00:02:20,510 --> 00:02:22,580 Pretty straightforward at this point. 37 00:02:22,580 --> 00:02:26,854 Now let's say a beginning programmer has accidentally added a string parameter to 38 00:02:26,854 --> 00:02:28,267 the toString() method. 39 00:02:30,044 --> 00:02:32,280 String something. 40 00:02:32,280 --> 00:02:37,170 You and I know that this isn't right, but the Java compiler is none the wiser. 41 00:02:37,170 --> 00:02:40,830 We would definitely see some puzzling behavior if we tried to use the toString() 42 00:02:40,830 --> 00:02:45,490 method by creating a new cheese object, then displaying it in the console. 43 00:02:45,490 --> 00:02:48,611 Let me switch to my main class in that same package and do just that. 44 00:02:51,629 --> 00:02:57,014 So, in the main class, we will create a new Cheese object, 45 00:02:57,014 --> 00:03:00,160 calling it default constructor. 46 00:03:01,320 --> 00:03:09,960 And then, with a simple System.out.println statement, display myCheese. 47 00:03:09,960 --> 00:03:12,630 Let's compile and run the application. 48 00:03:12,630 --> 00:03:16,490 I'll change directories to src, standing for source. 49 00:03:18,060 --> 00:03:22,320 To do this, I'll use the cd command, which stands for change directory. 50 00:03:22,320 --> 00:03:26,897 Then, I'll compile our code with a javac command being sure to list the full path 51 00:03:26,897 --> 00:03:27,988 to our main class. 52 00:03:35,533 --> 00:03:37,490 Looks like it complies successfully. 53 00:03:37,490 --> 00:03:40,090 So I'll run our application with the java command, 54 00:03:40,090 --> 00:03:43,118 using the dot notation that includes the full package name. 55 00:03:51,259 --> 00:03:54,784 The output we're getting is the cheese object's hash code, 56 00:03:54,784 --> 00:03:58,650 which means our toString() method wasn't actually called, but 57 00:03:58,650 --> 00:04:01,530 rather the Java object's toString() method. 58 00:04:01,530 --> 00:04:04,440 This is a logical error that occurs at run time. 59 00:04:04,440 --> 00:04:08,770 If we ever have a chance to convert runtime errors into compile time errors, 60 00:04:08,770 --> 00:04:10,290 we'll take it. 61 00:04:10,290 --> 00:04:14,380 And, I'd rather fix a compiler before releasing my software, 62 00:04:14,380 --> 00:04:17,290 than upset a user with a pesky runtime error. 63 00:04:17,290 --> 00:04:21,580 By simply adding the override annotation to our toString() method, 64 00:04:21,580 --> 00:04:24,900 the Java compiler will perform a check on inheritance. 65 00:04:24,900 --> 00:04:29,810 Notice that our method does not constitute proper overriding and generate an error. 66 00:04:29,810 --> 00:04:34,796 So, we'll add the annotation, go back to my 67 00:04:34,796 --> 00:04:39,916 Cheese class, add the override annotation, 68 00:04:39,916 --> 00:04:46,190 go back down to our console, recompile, and voila. 69 00:04:46,190 --> 00:04:48,740 We have now detected our error during compile time.