Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
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.
Find answers to the most frequently asked questions about Java Essential Training (2011) .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
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.