2 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
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.
If a subclass’s constructor doesn't explicitly call a superclass’s constructor using super, Java will insert a call to the superclass’s no-argument constructor.
We will use super in the following scenario. We have a Quadrilateral superclass with a constructor, an area() method, and an isEquivalent() method. We have the method headers written in the quadrilateral class, but we won't show the implementation.
/** 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](https://www.fiveableKeyTerm: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.
If Quadrilateral was a subclass of another class, say the Shape class, the constructor for Quadrilateral could use super to call the constructor for the Shape class. The process of calling superclass constructors continues until the Object constructor is called, regardless of whether the superclass constructor is called implicitly or explicitly. The Object class is the ultimate superclass of all classes in Java. At this point, all of the constructors within the hierarchy would execute, starting with the Object constructor.
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.
Term 1 of 10
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.
Term 1 of 10
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.
Term 1 of 10
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.
Subclass: A subclass is a class that extends another class (superclass). It inherits all the attributes and behaviors from its superclass while adding its own additional features.
Inheritance: Inheritance is the mechanism in Java where one class acquires properties (fields and methods) from another class. It allows code reuse and promotes hierarchical organization of classes.
Method Overriding: Method overriding occurs when a subclass provides its own implementation of a method that already exists in its superclass. The overridden method in the subclass replaces the original implementation provided by the superclass.
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.
Superclass: The superclass is the class from which the subclass inherits properties and behaviors.
Inherit: Inheritance is the process by which one class acquires properties and behaviors from another class.
Polymorphism: Polymorphism allows objects of different classes to be treated as objects of the same superclass, enabling flexibility in programming.
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.
Instance Variables: Instance variables are attributes or properties associated with objects of a class. They hold unique values for each instance/object.
Default Constructor: A default constructor is a constructor that takes no arguments and provides default values for instance variables if not explicitly initialized.
Parameterized Constructor: A parameterized constructor is a constructor that accepts parameters/arguments during object creation and uses those values to initialize instance variables.
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.
Constructors: Constructors are special methods used to initialize objects. When creating a subclass, you can use the super keyword to call the constructor of its superclass.
Subclasses: Subclasses are classes that inherit properties and behaviors from another class called the superclass. The super keyword is often used in subclasses to access members of their superclass.
Inheritance: Inheritance is a concept in object-oriented programming where one class inherits properties and behaviors from another class. The super keyword plays an important role in implementing inheritance by allowing access to superclass members.
A quadrilateral is a polygon with four sides. It is characterized by having four vertices (corners) and four angles.
Polygon: A polygon is any closed figure with straight sides. Quadrilaterals are one type of polygon.
Parallelogram: A parallelogram is a special type of quadrilateral where opposite sides are parallel.
Trapezoid: A trapezoid is another type of quadrilateral with only one pair of parallel sides.
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.
Method: A block of code that performs a specific task. It can be called upon to execute its functionality.
Return statement: A statement used in programming languages to return a value from a function or method.
Object-oriented programming (OOP): A programming paradigm based on objects, where objects are instances of classes containing data (attributes) and behavior (methods).
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.
Comparison operator: An operator used in programming languages to compare two values or expressions.
Boolean data type: A data type that represents either true or false values.
Equality operator: An operator used to compare two values for equality, typically returning a boolean value.
A rectangle is a specific type of quadrilateral with four right angles (90 degrees). It has opposite sides that are equal in length.
Square: A square is a special type of rectangle where all four sides are equal in length.
Perimeter: The perimeter of a rectangle is the total distance around its outer edges, calculated by adding up the lengths of all four sides.
Area: The area of a rectangle is the amount of space it occupies and can be calculated by multiplying the length and width of the rectangle.
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.
Inheritance: Inheritance is when one class inherits the properties and behaviors of another class. For example, a "Car" class can inherit from a more general "Vehicle" class.
Encapsulation: Encapsulation is the bundling of data and methods within an object. It allows for better organization and control over access to the data.
Polymorphism: Polymorphism refers to the ability of an object to take on many forms. It allows different classes to be used interchangeably through inheritance or interfaces.