CMSC 132A Lecture 13: Super Keyword

51 views4 pages

Document Summary

If your method overrides one of its superclass"s methods, you can invoke the overridden method through the use of the keyword super . You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, superclass : public class superclass { public void printmethod() { Here is a subclass, called subclass , that overrides printmethod() : public class subclass extends superclass { // overrides printmethod in superclass public void printmethod() { super. printmethod(); System. out. println("printed in subclass"); public static void main(string[] args) { Within subclass , the simple name printmethod() refers to the one declared in. So, to refer to printmethod() inherited from superclass , subclass must use a qualified name, using super as shown. The following example illustrates how to use the super keyword to invoke a superclass"s constructor. Recall from the bicycle example that mountainbike is a subclass of bicycle .

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents