![]() ![]() You can go ahead and select this and NetBeans will add annotation above the method. If you hover over the lightbulb, NetBeans asks you if it can add an 'override' annotation. If you look to the left of this method, you'll see the yellow light bulb and a black arrow pointing down. ![]() To do so, we will override this method by specifying a new implementation. We would like this method to display additional information for a Student object. Why do we need to define it again here? This is because we are not satisfied with the display() method in the Person class, which only prints out a person's name and id. Open the Student class and add the display()method at the end of the class. When Java is compiling the code it can tell that the subclass is overriding the method from the superclass and will instead only use the subclass's version of that method. Well, you can just override it, by writing a new method with the same name in the subclass. What may have been appropriate general behavior for the super class may no longer be appropriate for the subclass. But as we move down the inheritance hierarchy, we get to more specialized classes. We saw the same behavior when this method was called regardless of the specific type of object used to invoke it. A Student/Professor object was used to invoke the display() method even though it was not explicitly added to those classes. We saw how a subclass has access to public methods derived from the parent (super) class. Both Student and Professor extends a common base class Person and has the methods getFirstName, getLastName, getId, and display. Recall the inheritance hierarchy of Person classes from the previous lab. Open the NetBeans Project you created in the previous lab. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |