super Keyword
The super
keyword is used in a subclass to provide access to fields or methods that are defined in the base class.
Consider these two classes:
public class Ball
{
public void hit()
{
System.out.println(“You hit it a mile!”);
}
}
class BaseBall extends Ball
{
public void hit()
{
System.out.println(“You tore the cover off!”);
super.hit();
}
}
Here, the hit
method in the BaseBall
class calls the hit
method of its base class object. Thus, if you call the hit
method of a BaseBall
object, the following two lines are displayed on the console:
You tore the cover off!
You hit it a mile!
Get Java For Dummies Quick Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.