Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When a subclass extends a superclass, it inherits the methods of that superclass. But it can override them replacing the version in the superclass; I'll show you how to do this with the Olive superclass and Kalamata subclass in this version of my application overriding methods. The olive superclass has a method called crush, it outputs a simple string and returns a value. Now, the particular example I'm going to show is very simple, but my goal is to show you the basic syntax and then as you find reasons to extend superclasses and override their methods you'll know how to do it.
Go to the subclass Kalamata and place the cursor after the constructor method. Now you could type all the code yourself, but Eclipse will help you, press Ctrl+Space and you'll see a list of all the methods of the superclass, choose the one you want to override crush, a new version of the crush method is added to your code. This annotation the @ character and the word Override with an uppercase O is not required, but it is recommended it makes your code more readable, it makes it clear that you're overriding the superclasses version of the method.
When you override the superclass version the data type must match. So if I go back to the superclass method it returns an int value and here's the subclass version, this arrow pointing upward over and the trough indicates that this is an overriding method and when you click it, it will take you to the superclass version, I'll go back to the subclass, I'll get rid of this TODO comment, we don't need that, the version of the class that Eclipse generates includes a statement return super. crush, this means called the superclasses version of the method and return whatever value it returns.
So, even though you're overriding, you still can take advantage of the functionality that's apart the superclass implementation. Now, I'll place the cursor inside the new method and I'll use System.out.println and I'll output the message "crush from subclass", then I'll go back to the superclass version of the method and I'll output "crush from superclass", I'll save all my changes and run the application and you'll see where ever the subclass version is being called that first to get the message from the subclass and then from the superclass.
And this is because of the order of the code first I'm getting this print command, then I'm calling the return statement and calling the super crush method, the super crush method is outputting its message returning the value and then finally the subclass method takes over and returns the value to the main application. So to review the rules for overriding methods the data type of the method that you're overriding must be repeated in the subclass, the at override annotation is not required but it is recommended, it makes your code more readable.
And once you know how to override methods from the superclass you can customize the behavior of your subclasses however your application requires.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99373 Viewers
56 Video lessons · 112631 Viewers
71 Video lessons · 81455 Viewers
131 Video lessons · 39104 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.