πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ–²

9.4 Super Keyword

1 min readβ€’october 19, 2021

Peter Cao

Peter Cao

Peter Cao

Peter Cao


AP Computer Science AΒ πŸ’»

130Β resources
See Units

Super Keyword

Other times, we are overriding or overloading a method, and then we want to borrow the superclass’s implementation of that method as a substep. 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()!
/** 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); } }
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–±Unit 10 – Recursion
πŸ™Exam Reviews