Please enable JavaScript to use CodeHS

Chapter 9

Inheritance

9.4 super Keyword

When a subclass object is created, a superclass object is created in the process. This is evident in the initial call to super that is made in the constructor:

public class Student extends Person { 
   private int grade;
   private double gpa;

   public Student (String name, String birthday, int grade, double gpa) {
      super(name,birthday);
      this.grade = grade; this.gpa = gpa;
   }

   public int getGrade(){
      return grade;
   }
} 
Plain text

In addition to creating the superclass object, the super keyword can be used to reference the super object in other methods within the subclass.

Using the super Keyword

The keyword super is similar to the keyword this. this refers to the current object, while super refers to the superclass object. Just as it’s possible to reference methods and pass parameters using the this keyword, methods can be referenced and passed parameters using the super keyword.

super refers to the superclass object and can call methods using:

super.methodName(parameters)
Plain text

Here is a sample superclass Animal:

public class Animal {

   private String type;
   private String sound;

   public Animal (String type, String sound) {
      this.type = type;
      this.sound = sound;
   }

   public void speak(int number){
      for (int i = 0; i < number; i++)
         System.out.println(sound);
      }
   }
Plain text

The Dog class extends the Animal class, inheriting its methods and instance variables:

public class Dog extends Animal {

   public Dog () {
      super("Dog", "Bark");
   }

   public void bark(int number){
      super.speak(number);
   }

}
Plain text

In this example, the Dog subclass constructor uses the super keyword to call the Animal constructor. The Dog class bark() method also calls the superclass speak() method by passing it a parameter that will then get used in the speak() method.

Animal Class

Square Class

Apple Pie

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    Given the following code, what is the correct way for the subclass to call the speak method in the Person class?

    public class Person {
    
        public void speak(String sound) {
            System.out.println(sound);
        }
    }
    
    public class EnglishSpeaker extends Person {
    
        public void greet() {
            /* Missing code */
        }
    }
    Java

Exercise: Bank Accounts

For this exercise, you will be completing the Account class, which simulates a regular bank account, then using overrides and calls to the superclass to create a StudentAccount.

Student accounts differ from regular accounts in that they get a 10% bonus for every deposit, but a $1.50 fee for every withdrawal. For instance, if you deposit $1.00 into a student account, your balance actually grows by $1.10 ($0.10 is 10% of $1.00). Likewise, if you withdraw $1.00, your balance actually shrinks by $2.50.

You will override the methods in the StudentAccount by calling the superclass methods with the additional amount or fee incorporated since the balance is not directly stored in the StudentAccount object.

You will also update the toString(), as outlined in the comments.

When completed, create one student account and one regular account for testing. Deposit and withdraw money and print the results.