Java Term of the Week: Super
This week's Java term is the super keyword. It enables a subclass to call the methods and fields of its superclass. It's important to note that it is not an instance of the superclass object. It's simply a way to tell the compiler which methods or fields to reference. The effect of using a super.methodname statement is the same as if the subclass is calling one of its own methods.
For example, if an Employee class extends a Person class:
public class Employee extends Person{
public Employee()
{
//reference the superclass constructor
super();
}
public String getName()
{
//reference superclass behaviors
return super.getFirstName() + " " + super.getLastName();
}
}
The super keyword can be used to reference the constructer of the Person class or any of the behaviors or fields that it has access to (e.g., getFirstName() and getLastName()).


No comments yet. Leave a Comment