Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

9.2 Writing Constructors for Subclasses

3 min readdecember 31, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

While methods and instance variables of a are carried over to a , are not. That means that we have to write our again!

The Super Keyword

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 in this topic and then with methods in the next topic.

Using the , we can call the ’s constructor and use that to construct an object of the .

If a ’s constructor doesn't explicitly call a ’s constructor using super, Java will insert a call to the ’s no-argument constructor.

Example

We will use super in the following scenario. We have a with a constructor, an , and an . We have the method headers written in the class, but we won't show the implementation.

/** Represents a 

Now, we want to make a that represents a . First, we will have to make the method header and constructor of the class:

/** Represents a 

By calling the super constructor here, we are using the constructor but using the parameters passed into the constructor as the constructor parameters. If the class has any extra instance variables not in the class, they would be initialized separately.

If was a of another class, say the Shape class, the constructor for could use super to call the constructor for the Shape class. The process of calling continues until the Object constructor is called, regardless of whether the constructor is called implicitly or explicitly. The is the ultimate of all classes in Java. At this point, all of the within the hierarchy would execute, starting with the Object constructor.

Key Terms to Review (10)

area() method

: The area() method is a function defined within a class that calculates and returns the area of an object, such as a shape or figure.

Constructors

: Constructors are special methods within classes that are used to initialize objects when they are created. They have the same name as the class and are called automatically when an object is instantiated.

extends

: In Java, the keyword "extends" is used to create a subclass that inherits properties and methods from a superclass. It establishes an "is-a" relationship between classes.

isEquivalent() method

: The isEquivalent() method is used to compare two objects or values to determine if they are equivalent or equal in some way. It typically returns true if they are equivalent, and false otherwise.

Object class

: The object class is a blueprint or template for creating objects in object-oriented programming. It defines the properties and behaviors that an object of that class will have.

Quadrilateral

: A quadrilateral is a polygon with four sides. It is characterized by having four vertices (corners) and four angles.

Rectangle

: A rectangle is a specific type of quadrilateral with four right angles (90 degrees). It has opposite sides that are equal in length.

Subclass

: A subclass is a class that inherits properties and behaviors from another class, called the superclass. It can add new features or modify existing ones.

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

3 min readdecember 31, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

While methods and instance variables of a are carried over to a , are not. That means that we have to write our again!

The Super Keyword

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 in this topic and then with methods in the next topic.

Using the , we can call the ’s constructor and use that to construct an object of the .

If a ’s constructor doesn't explicitly call a ’s constructor using super, Java will insert a call to the ’s no-argument constructor.

Example

We will use super in the following scenario. We have a with a constructor, an , and an . We have the method headers written in the class, but we won't show the implementation.

/** Represents a 

Now, we want to make a that represents a . First, we will have to make the method header and constructor of the class:

/** Represents a 

By calling the super constructor here, we are using the constructor but using the parameters passed into the constructor as the constructor parameters. If the class has any extra instance variables not in the class, they would be initialized separately.

If was a of another class, say the Shape class, the constructor for could use super to call the constructor for the Shape class. The process of calling continues until the Object constructor is called, regardless of whether the constructor is called implicitly or explicitly. The is the ultimate of all classes in Java. At this point, all of the within the hierarchy would execute, starting with the Object constructor.

Key Terms to Review (10)

area() method

: The area() method is a function defined within a class that calculates and returns the area of an object, such as a shape or figure.

Constructors

: Constructors are special methods within classes that are used to initialize objects when they are created. They have the same name as the class and are called automatically when an object is instantiated.

extends

: In Java, the keyword "extends" is used to create a subclass that inherits properties and methods from a superclass. It establishes an "is-a" relationship between classes.

isEquivalent() method

: The isEquivalent() method is used to compare two objects or values to determine if they are equivalent or equal in some way. It typically returns true if they are equivalent, and false otherwise.

Object class

: The object class is a blueprint or template for creating objects in object-oriented programming. It defines the properties and behaviors that an object of that class will have.

Quadrilateral

: A quadrilateral is a polygon with four sides. It is characterized by having four vertices (corners) and four angles.

Rectangle

: A rectangle is a specific type of quadrilateral with four right angles (90 degrees). It has opposite sides that are equal in length.

Subclass

: A subclass is a class that inherits properties and behaviors from another class, called the superclass. It can add new features or modify existing ones.

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.