###### Bookmarks

Subjects and resources that you bookmark will appear here.

Light Mode

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.2 Writing Constructors for Subclasses

Peter Cao

While methods and instance variables of a superclass are carried over to a subclass, constructors are not. That means that we have to write our constructors again! Luckily, we don't have to start over from scratch. We have a special keyword that can help us. This is the super keyword. We will learn how to use it with constructors in this topic and then with methods in the next topic.
Using the super keyword, we can call the superclassβs constructor and use that to construct an object of the subclass.
We will do that in the following scenario. We will have a Quadrilateral superclass with a constructor, an area() method, and an isEquivalent() method. We will have the method headers written in the quadrilateral class, but without the implementation shown as follows:

## Java Example

```/** Represents a quadrilateral */ public class Quadrilateral { double sideOne; double sideTwo; double sideThree; double sideFour; /** Makes a quadrilateral with 4 sides of length sideOne, sideTwo, sideThree, sideFour */ public Quadrilateral(double sideOne, double sideTwo, double sideThree, double sideFour) { } /** Calculates the area of the quadrilateral */ public double area() { } /** Determines whether another quadrilateral with given side lengths is the same as the object we have */ public boolean isEquivalent(double sideOne, double sideTwo, double sideThree, double sideFour) { } }```
Now, we want to make a subclass that represents a Rectangle. First, we will have to make the method header and constructor of the Rectangle class:
```/** 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); } }```
By calling the super constructor here, we are using the Quadrilateral constructor but using the parameters passed into the Rectangle constructor as the Quadrilateral constructor parameters. If the Rectangle class has any extra instance variables not in the Quadrilateral class, they would be initialized separately.

##### πͺπ½ 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
Studying with Hours = the ultimate focus mode
Start a free study session
##### FREE AP comp sci a Survival Pack + Cram Chart PDF
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