Java Super: A Guide
Inheritance is a core feature in object-oriented programming that allows a developer to define a new class from an existing class. For instance, a class to store the suppliers of meat at a local health foods store could be defined based on a class to store any product sold at the store.
When you’re working with inheritance in Java, you’ll likely encounter the super keyword. The super keyword is used to access superclass members inside a subclass, and has a variety of uses when inheriting superclass members.
This tutorial will discuss, with references and examples, the basics of the Java super keyword and how you can use it in your code. By the end of reading this tutorial, you’ll be a master at using the super keyword in Java.
Java Inheritance
In Java, inheritance is used to define a new class from an existing one. Inheritance is a useful feature because it allows coders to reduce the amount of code they have to repeat because they can use existing code to create new classes.
Inheritance is used when there is an is-a
relationship between two classes. Here are a few examples of this type of relationship:
- Meat is a food
- An iPhone is a phone
- A lamp is an item of furniture
- A tire is part of a car
By inheriting code from another method, you can access the attributes, constructors, and methods stored in that function and use those in your new method. Here’s an example of inheritance being used to inherit the code stored in a BankAccount method for use in a new method called SavingsAccount:
class BankAccount { // Methods, attributes, constructors } class SavingsAccount extends BankAccount { // Methods, attributes, constructors }
In this example, we use the extends keyword to inherit all the methods, attributes, and constructors from the BankAccount method and apply them to the new SavingsAccount method. If you’re interested in learning more about Java inheritance, you can read our full guide on the topic here.
Java Super
The super keyword is used to access superclass members in a subclass. The members of a superclass are constructors, attributes, and methods.
In Java, the superclass, also known as the parent class
, is the class from which a child class (or a subclass) inherits its constructors, methods, and attributes. For instance, in our above example, BankAccount was the superclass from which our subclass SavingsAccount inherited its values.
The super keyword is used in three situations, which we will explore below.
Using Super to Access Superclass Attributes
In Java, attributes in a superclass and subclass can have the same name. If you want to explicitly access the contents of a superclass’ attribute, you have to use the super keyword.
Here’s an example of super being used with reference to our bank account example from above to access a class attribute:
class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }
Our code returns:
Account type: Savings Account Parent account type: Bank Account
In our example, we have declared a superclass called BankAccount which has one attribute: accountType. accountType is equal to Bank Account
in this class.
We have also defined a subclass called SavingsAccount which inherits its values from BankAccount. In this subclass, we declare a variable called accountType which stores the type of account held by that class, which is Savings Account
in this case.
Then we create a method called printAccountDetails()
which, when invoked, prints out the account type of the SavingsAccount, preceded by the label Account type:
. We use the accountType variable to print out the account type of SavingsAccount because the accountType variable has local scope.
After that has been printed, Parent account type:
is printed to the console, followed by the accountType of the BankAccount class. We use super.accountType
to reference the value of accountType which is stored in the parent class default constructor.
In our main program, we first create an object of the SavingsAccount method called johnAccount which stores the details for his account. Then we call the method called printAccountDetails()
to retrieve information about John’s account.
Using Super to Access Superclass Method
Methods in a subclass and a superclass can have the same name. If you want to access the method stored in a superclass that has the same name as a subclass method, you have to use the super keyword.
Here’s an example of super being used to access a superclass method, using the example of a program that stores bank account information:
class BankAccount { public void displayAccountType() { System.out.println("Account type: Bank Account"); } } class SavingsAccount extends BankAccount { @Override public void displayAccountType() { System.out.println("Account type: Savings Account"); } public void displayParentAccountType() { super.displayAccountType(); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.displayAccountType(); johnSavings.displayParentAccountType(); } }
Our code returns:
Account type: Savings Account Account type: Bank Account
In this example, we have declared a superclass called BankAccount which has one method: displayAccountType()
. When this method is invoked, Account type: Bank Account
is printed to the console.
We have also declared a subclass called SavingsAccount which has two methods: displayAccountType()
and displayParentAccountType()
.
When displayAccountType()
is executed, Account type: Savings Account
is printed to the console. When displayParentAccountType()
is executed, we use the super keyword to invoke the displayAccountType()
method from our parent class. So, instead of executing the displayAccountType()
method in the SavingsAccount class, our program executes the displayAccountType()
method in the BankAccount class.
In our main program, we first declare an instance of SavingsAccount called johnSavings. We then invoke the displayAccountType()
method, which runs displayAccountType()
from the SavingsAccount method. Finally, we invoke displayParentAccountType()
which executes super.displayAccountType()
and prints Account type: Bank Account
to the console.
Using Super to Access Superclass Constructor
The super keyword is also used to access a constructor from a superclass that shares the same name as a constructor in a subclass. You can learn more about Java constructors in our Java constructor tutorial here.
To call a superclass constructor from a subclass constructor, we need to use super()
inside the subclass constructor. Super()
must be the first statement in the argument constructor.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Here’s an example of super()
being used to call a superclass constructor, using our bank account example application:
class BankAccount { BankAccount() { System.out.println("Parent account type: Bank Account"); } } class SavingsAccount extends BankAccount { SavingsAccount() { super(); System.out.println("Main account type: Savings Account"); } } class Main { public static void main(String[] args) { SavingsAccount johnAccount = new SavingsAccount(); } }
Our code returns:
Parent account type: Bank Account Main account type: Savings Account
In this example, we have defined a superclass called BankAccount which includes a constructor. This constructor, when invoked, prints Parent account type: Bank Account
to the console.
We have also defined a subclass called SavingsAccount which includes a constructor. This constructor, when invoked, uses super()
to invoke the BankAccount constructor in the BankAccount class. This prints Parent account type: Bank Account
to the console. Then, the SavingsAccount constructor prints Main account type: Savings Account
to the console.
In our main program, we initialize an instance of the SavingsAccount object called johnAccount. When we initialize johnAccount, the SavingsAccount()
constructor is executed.
Conclusion
The Java super keyword has three uses. These are to:
- Access superclass attributes
- Access superclass methods
- Access superclass constructors
This tutorial discussed, with reference to examples, how to use the Java superclass keyword for those purposes. Now, after reading this tutorial, you’re ready to start using the Java super keyword like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.