The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!

πŸ“š

All Subjects

Β >Β 

πŸ’»Β 

AP Comp Sci A

Β >Β 

πŸ–²

Unit 9

9.3 Overriding Methods

1 min readβ€’november 16, 2020

Peter Cao


Now that we know how to make subclasses and their constructors, it is time to write their methods. Most methods that are inherited from the superclass do not need to be rewritten. However, some methods will have to be rewritten because their implementations will be different. This can be done by overloading, where the parameters for the methods are different but the methods achieve the same thing.

Overriding

The other way of doing this is overriding, which is when the method will have the same parameters, but their implementations will be different. Above the method header, we have to write @Override to show that this is an overridden method. For an overridden method, we usually don't have to write Javadoc comments since those should be inherited from the superclass. In the Rectangle class, we will override the area method as follows:
/** 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 } }

Was this guide helpful?

πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Browse Study Guides By Unit
πŸ™
Exam Reviews
πŸ–±
Unit 10: Recursion
βž•
Unit 1: Primitive Types
πŸ“±
Unit 2: Using Objects
πŸ–₯
Unit 3: Boolean Expressions and if Statements
πŸ•Ή
Unit 4: Iteration
βš™οΈ
Unit 5: Writing Classes
⌚️
Unit 6: Array
πŸ’Ύ
Unit 7: ArrayList
πŸ’»
Unit 8: 2D Array
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
πŸ“± Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741
Β© 2021 Fiveable, Inc.