June 2012
Intermediate to advanced
288 pages
6h 48m
English
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!
Read now
Unlock full access