The @Override Annotation4:49 with Chris Ramacciotti
@Override annotation in Java instructs the compiler to verify proper inheritance. Specifically, it verifies proper overriding of methods. In this video, we'll see this annotation in action.
The first annotation we're going to look at is called override. 0:00 Now, you can't get too far in Java without running into this one. 0:04 The override annotation is processed and discarded when your code is compiled. 0:08 Essentially what it does, is send a message to the Java compiler, 0:13 asking hey, when you compile this code can you tell me whether or 0:16 not this method I've annotated properly overrides a method. 0:20 If the answer is no, then the compiler will produce an error. 0:25 It is good practice to use this annotation 0:29 because it allows you to use the compiler to verify proper inheritance. 0:31 One of the most common examples of using the override annotation 0:36 would be a toString() method. 0:39 Let's experiment with writing with one of these, and see how we can 0:42 us the override annotation to ensure that we are properly overriding a method. 0:45 In this workspace I have a simple Java project with a few packages. 0:51 You can find a link to the project in the teacher notes if you'd like to download it 0:54 and import it to your favorite IDE, such as Intel JRE Eclipse. 0:58 For this course, I'll stick with workspaces. 1:02 To demonstrate the use of the override annotation, 1:05 we'll make a simple class in the override package. 1:08 How about we call it, cheese? 1:10 We'll create a new file. 1:14 Name it Cheese.Java. 1:18 And we'll set up the structure of our class here, public class Cheese. 1:22 Let's not forget our package. 1:29 Brilliant, now we won't go into too much detail crafting the perfect cheese. 1:39 There are many fine cheeses out there, so 1:43 we don't need to go reinventing the wheel, get it? 1:45 Wheel, like a cheese wheel? 1:48 Oh, nevermind. 1:51 Anyway, one of the most common tasks we wanna perform with an object 1:54 is to display a string summary of the object. 1:57 You've likely learned that this is accomplished via the toString() method. 2:00 Let's write a simple one that returns a hard coded string. 2:03 We'll start with out signature, public string toString(). 2:07 And we will return a simple string that says, 'String cheese'. 2:12 Pretty straightforward at this point. 2:20 Now let's say a beginning programmer has accidentally added a string parameter to 2:22 the toString() method. 2:26 String something. 2:30 You and I know that this isn't right, but the Java compiler is none the wiser. 2:32 We would definitely see some puzzling behavior if we tried to use the toString() 2:37 method by creating a new cheese object, then displaying it in the console. 2:40 Let me switch to my main class in that same package and do just that. 2:45 So, in the main class, we will create a new Cheese object, 2:51 calling it default constructor. 2:57 And then, with a simple System.out.println statement, display myCheese. 3:01 Let's compile and run the application. 3:09 I'll change directories to src, standing for source. 3:12 To do this, I'll use the cd command, which stands for change directory. 3:18 Then, I'll compile our code with a javac command being sure to list the full path 3:22 to our main class. 3:26 Looks like it complies successfully. 3:35 So I'll run our application with the java command, 3:37 using the dot notation that includes the full package name. 3:40 The output we're getting is the cheese object's hash code, 3:51 which means our toString() method wasn't actually called, but 3:54 rather the Java object's toString() method. 3:58 This is a logical error that occurs at run time. 4:01 If we ever have a chance to convert runtime errors into compile time errors, 4:04 we'll take it. 4:08 And, I'd rather fix a compiler before releasing my software, 4:10 than upset a user with a pesky runtime error. 4:14 By simply adding the override annotation to our toString() method, 4:17 the Java compiler will perform a check on inheritance. 4:21 Notice that our method does not constitute proper overriding and generate an error. 4:24 So, we'll add the annotation, go back to my 4:29 Cheese class, add the override annotation, 4:34 go back down to our console, recompile, and voila. 4:39 We have now detected our error during compile time. 4:46
You need to sign up for Treehouse in order to download course files.Sign up