The super Keyword
Part of the Object Oriented Programming section of Coddy's Java journey — lesson 21 of 87.
The super keyword provides a way for a subclass to reference its parent class. It's essential when you need to access parent class members or constructors from within a child class.
The most common use of super is calling the parent's constructor. This must be the first statement in the subclass constructor:
public class Vehicle {
private String brand;
public Vehicle(String brand) {
this.brand = brand;
}
public String getBrand() {
return brand;
}
}
public class Car extends Vehicle {
private int doors;
public Car(String brand, int doors) {
super(brand); // Must be first line
this.doors = doors;
}
}You can also use super to call parent methods, which is particularly useful when the subclass has a method with the same name:
public class Animal {
public void makeSound() {
System.out.println("Some sound");
}
}
public class Cat extends Animal {
public void makeSound() {
super.makeSound(); // Calls Animal's version
System.out.println("Meow!");
}
}Think of super as the opposite of this. While this refers to the current object, super refers to the parent class portion of that object. This distinction becomes crucial when building class hierarchies where child classes need to extend rather than completely replace parent behavior.
Challenge
EasyLet's build an appliance system that demonstrates how child classes can use super to call parent constructors and extend parent method behavior.
You'll create three files to organize your code:
Appliance.java: Create the parent class representing any household appliance. It should have:- A private field for the appliance's
brand(String) - A private field for
wattage(int) - A constructor that accepts both values and initializes them
- A method
getBrand()that returns the brand - A method
getWattage()that returns the wattage - A method
getInfo()that returns:[brand] - [wattage]W
- A private field for the appliance's
WashingMachine.java: Create a subclass that extends Appliance. A washing machine has everything an appliance has, plus its own special features:- A private field for
capacity(int) representing load capacity in kg - A constructor that takes brand, wattage, and capacity - use
super(brand, wattage)to initialize the parent portion, then set the capacity - A method
getCapacity()that returns the capacity - A method
getInfo()that calls the parent'sgetInfo()usingsuper.getInfo()and extends it to return:[parent info], Capacity: [capacity]kg
- A private field for
Main.java: Create a WashingMachine and display its complete information. You'll receive three inputs: the brand (String), wattage (int), and capacity (int). Print the result of callinggetInfo()on your washing machine.
You will receive three inputs: brand name, wattage, and capacity.
Notice how super serves two purposes here: calling the parent constructor to initialize inherited fields, and calling the parent's method to build upon its behavior rather than replacing it entirely.
Cheat sheet
The super keyword references the parent class from within a subclass.
Use super() to call the parent constructor. This must be the first statement in the subclass constructor:
public class Vehicle {
private String brand;
public Vehicle(String brand) {
this.brand = brand;
}
}
public class Car extends Vehicle {
private int doors;
public Car(String brand, int doors) {
super(brand); // Must be first line
this.doors = doors;
}
}Use super.methodName() to call parent methods, useful when the subclass overrides a method:
public class Animal {
public void makeSound() {
System.out.println("Some sound");
}
}
public class Cat extends Animal {
public void makeSound() {
super.makeSound(); // Calls Animal's version
System.out.println("Meow!");
}
}super refers to the parent class portion of an object, while this refers to the current object.
Try it yourself
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read inputs
String brand = scanner.nextLine();
int wattage = scanner.nextInt();
int capacity = scanner.nextInt();
// TODO: Create a WashingMachine object with the input values
// TODO: Print the result of calling getInfo() on the washing machine
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System