Sub topic 5
Call Superclass Methods and Access Fields with super
When one class extends another, the new class is called a subclass. The class it extends is called the superclass. A subclass gets the fields and methods of its superclass. Sometimes a subclass changes a method, but it still needs the original version. The keyword super lets you reach the superclass from inside the subclass.
A keyword is a word that Java reserves for its own use. You cannot use it as a variable name.
Calling a Superclass Method
Suppose you want a Dog to say everything an Animal says, plus something extra. You can write super.describe() to run the describe() method from Animal. Then you add your own text.
class Animal {
protected String name;
Animal(String name) {
this.name = name;
}
String describe() {
return "This animal is named " + name;
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
@Override
String describe() {
return super.describe() + " and it is a dog";
}
void printName() {
System.out.println("Dog name field: " + super.name);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Rex");
System.out.println(dog.describe());
dog.printName();
}
}The output is:
This animal is named Rex and it is a dog
Dog name field: RexHere is what happens when main calls dog.describe():
diagram mermaid caption: Sequence of calls when Dog.describe() uses super.describe() alt: Main calls describe on Dog. Dog calls describe on Animal using super. Animal returns its text to Dog, Dog adds its own text, and then Dog returns the full text to Main. sequenceDiagram participant Main participant Dog participant Animal Main->>Dog: describe() Dog->>Animal: super.describe() Animal-->>Dog: "This animal is named Rex" Dog-->>Main: "... and it is a dog"
The line Animal(String name) is a constructor, which is the code that sets up a new object. The line super(name) in Dog calls that constructor. You will learn more about it in the next chapter.
Accessing a Superclass Field
A field is a variable that belongs to an object. In the code above, name is a field. The protected label means that subclasses can see the field. In printName(), super.name reads the field from Animal.
callout tip If Dog has its own field also called name, super.name reaches the parent's field and this.name reaches the subclass's field. Using both names can make code hard to read, so pick clear names. Keep in mind: a field marked private cannot be reached with super.name. Use a public getter method instead.
Quick Reference
| Form | What it reaches |
|---|---|
| `super.methodName()` | A method from the superclass |
| `super.fieldName` | A field from the superclass |
| `super(...)` | A superclass constructor, and only as the first line of a constructor |
callout note super always points one level up, to the direct superclass. It does not skip over a parent to reach a grandparent.
Try It
exercise Q: Using the code above, what does this line print? System.out.println(new Dog("Mia").describe()); A: It prints This animal is named Mia and it is a dog. The Dog version of describe() calls super.describe(), which returns the Animal text, and then adds " and it is a dog".
estimate minutes: 20