9.4 Super Keyword
Using the super keyword to call a superclass’s method.
public class Performer { //superclass
public void practice(){
System.out.println("Honing my craft!");
}
public void perform(){
System.out.println("Performing for an audience!");
}
}
public class Dancer extends Performer { //subclass
public void perform(){
System.out.println("Dancing on the stage!");
}
}
public class BalletDancer extends Dancer { //subclass
public void jete(){
System.out.println("Leaping...");
}
public void pirouette(){
System.out.println("Spinning...");
}
public void perform(){
jete();
pirouette();
}
public static void main(String[] args){
BalletDancer derrick = new BalletDancer();
derrick.practice();
derrick.perform();
}
}
System.out.println("BalletDancer class: ");
BalletDancer.main(null);
BalletDancer class:
Honing my craft!
Leaping...
Spinning...
public class Performer { //superclass of Dancer class
public void practice(){
System.out.println("Honing my craft!");
}
public void perform(){
System.out.println("Performing for an audience!");
}
}
public class Dancer extends Performer { //superclass of BalletDancer class
public void perform(){
System.out.println("Dancing on the stage!");
}
}
public class BalletDancer extends Dancer {
public void jete(){
System.out.println("Leaping...");
}
public void pirouette(){
System.out.println("Spinning...");
}
public void perform(){
perform();//Why is this wrong?
jete();
pirouette();
}
public static void main(String[] args){
BalletDancer derrick = new BalletDancer();
derrick.practice();
derrick.perform();
}
}
System.out.println("BalletDancer class: ");
BalletDancer.main(null);
Note:
Super keyword can be placed in any order as it prints out chronologically.
public void perform(){
super.perform();
jete();
pirouette();
}
will print out…
Honing my craft!
Dancing on the stage!
Leaping...
Spinning...
However, if we were to write:
public void perform(){
jete();
pirouette();
super.perform();
}
It will print out…
Honing my craft!
Leaping...
Spinning...
Dancing on the stage!
What if we use 2 super keywords?
public class Performer { //superclass
public void practice(){
System.out.println("Honing my craft!");
}
public void perform(){
System.out.println("Performing for an audience!");
}
}
public class Dancer extends Performer { //subclass
public void perform(){
super.perform();
System.out.println("Dancing on the stage!");
}
}
public class BalletDancer extends Dancer { //subclass
public void jete(){
System.out.println("Leaping...");
}
public void pirouette(){
System.out.println("Spinning...");
}
public void perform(){
super.perform();
jete();
pirouette();
}
public static void main(String[] args){
BalletDancer derrick = new BalletDancer();
derrick.practice();
derrick.perform();
}
}
System.out.println("BalletDancer class: ");
BalletDancer.main(null);
BalletDancer class:
Honing my craft!
Performing for an audience!
Dancing on the stage!
Leaping...
Spinning...
Popcorn Hack
Create a subclass and a superclass by calling the methods from the superclass from the subclass using the keyword super.