Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

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);
  }
}

Key Terms to Review (9)

Area method

: The area method is a technique used in computer science to calculate the size or complexity of an algorithm by counting the number of basic operations performed. It helps determine the efficiency and performance of an algorithm.

Inheritance

: Inheritance is a concept in object-oriented programming where a class inherits the properties and behaviors of another class. It allows for code reuse and promotes the creation of hierarchical relationships between classes.

isEquivalent method

: The isEquivalent method is a function used in computer programming that compares two objects or values and determines if they are equal in terms of their properties or attributes. It returns true if they are equivalent and false otherwise.

Overloading

: Overloading refers to defining multiple methods with the same name but different parameters within a single class. The methods must have different parameter lists or different types of parameters.

Overriding

: Overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The overridden method in the subclass replaces the implementation inherited from the superclass.

Quadrilateral Class

: A quadrilateral class is a general class in object-oriented programming that represents a four-sided polygon. It serves as the parent class for more specific types of quadrilaterals, such as rectangles, squares, and parallelograms.

Rectangle Class

: A rectangle class is a specific type of shape defined in object-oriented programming that represents a four-sided polygon with opposite sides being equal in length and all angles being right angles.

super keyword

: The super keyword is used in Java to refer to the superclass of a subclass. It allows access to the superclass's methods, constructors, and instance variables.

Superclass

: A superclass, also known as a parent class or base class, is a class that is extended by another class (subclass). It provides common attributes and behaviors that can be inherited by its subclasses.

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);
  }
}

Key Terms to Review (9)

Area method

: The area method is a technique used in computer science to calculate the size or complexity of an algorithm by counting the number of basic operations performed. It helps determine the efficiency and performance of an algorithm.

Inheritance

: Inheritance is a concept in object-oriented programming where a class inherits the properties and behaviors of another class. It allows for code reuse and promotes the creation of hierarchical relationships between classes.

isEquivalent method

: The isEquivalent method is a function used in computer programming that compares two objects or values and determines if they are equal in terms of their properties or attributes. It returns true if they are equivalent and false otherwise.

Overloading

: Overloading refers to defining multiple methods with the same name but different parameters within a single class. The methods must have different parameter lists or different types of parameters.

Overriding

: Overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The overridden method in the subclass replaces the implementation inherited from the superclass.

Quadrilateral Class

: A quadrilateral class is a general class in object-oriented programming that represents a four-sided polygon. It serves as the parent class for more specific types of quadrilaterals, such as rectangles, squares, and parallelograms.

Rectangle Class

: A rectangle class is a specific type of shape defined in object-oriented programming that represents a four-sided polygon with opposite sides being equal in length and all angles being right angles.

super keyword

: The super keyword is used in Java to refer to the superclass of a subclass. It allows access to the superclass's methods, constructors, and instance variables.

Superclass

: A superclass, also known as a parent class or base class, is a class that is extended by another class (subclass). It provides common attributes and behaviors that can be inherited by its subclasses.


© 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.