Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

9.4 Super Keyword

2 min readjanuary 2, 2023

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

When we talked about writing constructors for subclasses in Topic 9.2, we described how you can use the super keyword to invoke constructor in the superclass. As we'll learn in this topic, we can also use the super keyword to call on methods from the superclass.

Super for Methods

Sometimes when we are overriding or overloading a method, we want to borrow the superclass’s implementation of that method as a substep. (If our subclass isn't doing anything different from the superclass's implementation of that method, then we just wouldn't override or overload the method, since our subclass would inherit the superclass's implementation.)

Like constructors, we can also use the super keyword. When using the super keyword, we are calling the superclass's implementation of the method. This allows us to not have to duplicate code twice across multiple classes, which will save you both time and space!

Let us finish this quick Rectangle class with an implementation of isEquivalent() that invokes the super keyword!

/** Represents a rectangle
*/
public class Rectangle extends Quadrilateral {

  /** Makes a rectangle given a length and width
  */
  public Rectangle(double length, double width) {
    super(length, width, length, width);
  }

  @Override
  public double Area() {
    return sideOne * sideTwo; // from the constructor length is sideOne and width is sideTwo
  }

  /** Determines whether a rectangle with a given length and width is equivalent to the given rectangle
  */
  public boolean isEquivalent(double length, double width) {
    return super.isEquivalent(length, width, length, width);
  }
}

9.4 Super Keyword

2 min readjanuary 2, 2023

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

When we talked about writing constructors for subclasses in Topic 9.2, we described how you can use the super keyword to invoke constructor in the superclass. As we'll learn in this topic, we can also use the super keyword to call on methods from the superclass.

Super for Methods

Sometimes when we are overriding or overloading a method, we want to borrow the superclass’s implementation of that method as a substep. (If our subclass isn't doing anything different from the superclass's implementation of that method, then we just wouldn't override or overload the method, since our subclass would inherit the superclass's implementation.)

Like constructors, we can also use the super keyword. When using the super keyword, we are calling the superclass's implementation of the method. This allows us to not have to duplicate code twice across multiple classes, which will save you both time and space!

Let us finish this quick Rectangle class with an implementation of isEquivalent() that invokes the super keyword!

/** Represents a rectangle
*/
public class Rectangle extends Quadrilateral {

  /** Makes a rectangle given a length and width
  */
  public Rectangle(double length, double width) {
    super(length, width, length, width);
  }

  @Override
  public double Area() {
    return sideOne * sideTwo; // from the constructor length is sideOne and width is sideTwo
  }

  /** Determines whether a rectangle with a given length and width is equivalent to the given rectangle
  */
  public boolean isEquivalent(double length, double width) {
    return super.isEquivalent(length, width, length, width);
  }
}


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.