Java
In this tutorial we will learn about super keyword in Java programming language.
In the previous tutorials we learned about Inheritance and how to access inherited variables and methods. Feel free to check that out.
super
We use the super keyword in two cases.
To call the constructor of the parent class from the child class we use the super keyword and then pass the list of parameters, if any.
Syntax:
class Parent { // constructor Parent() { // some code... } } class Child extends Parent { // constructor Child() { super(); // some code... } }
The super() statement must be the first statement in the child constructor to call the parent constructor.
super()
In the following example we have the parent class Person and the child class Employee. From inside the child class we are calling the constructor of the parent class.
Person
Employee
// the parent class class Person { // general member variables String firstname; String lastname; // constructor Person(String firstname, String lastname) { this.firstname = firstname; this.lastname = lastname; } // general method public void showName() { System.out.println("First name: " + this.firstname); System.out.println("Last name: " + this.lastname); } } // the child class inheriting the parent class class Employee extends Person { // specific member variables String employeeid; int payscale; String joiningDate; // constructor Employee(String firstname, String lastname, String employeeid, int payscale, String joiningDate) { // calling the constructor of the parent class Person // to set the first name and last name super(firstname, lastname); // now setting the member variables of this class this.employeeid = employeeid; this.payscale = payscale; this.joiningDate = joiningDate; } // show employee details public void showDetail() { System.out.println("Employee details:"); // calling the inherited method showName() // of the parent class Person // to show the first name and last name this.showName(); // now calling the method of this class this.showEmployeeDetail(); } public void showEmployeeDetail() { System.out.println("Employee ID: " + this.employeeid); System.out.println("Pay scale: " + this.payscale); System.out.println("Joining Date: " + this.joiningDate); } } // the main class public class Example { // the main method public static void main(String[] args) { // employee data String firstname = "Yusuf"; String lastname = "Shakeel"; String employeeid = "E01"; int payscale = 3; String joiningDate = "2010-01-01"; // creating an object of the Employee class Employee empObj = new Employee(firstname, lastname, employeeid, payscale, joiningDate); // show detail empObj.showDetail(); } }
Output:
$ javac Example.java $ java Example Employee details: First name: Yusuf Last name: Shakeel Employee ID: E01 Pay scale: 3 Joining Date: 2010-01-01
Another use of the super keyword is to access the member of the parent class that is hidden in the child class.
Note! If a child class member variable shares the same name as the parent class member variable then the parent variable gets hidden in the child class.
Example:
class Parent { int x; } class Child extends Parent { int x; }
In the above code the variable x in the Child class is hiding the variable x of the Parent class.
x
Child
Parent
In the following example we are using the super keyword to access the member variable of the parent class from the child class.
// the parent class class Person { // general member variables String firstname; String lastname; // constructor Person(String firstname, String lastname) { this.firstname = firstname; this.lastname = lastname; } } // the child class inheriting the parent class class Employee extends Person { // member variable "firstname" of this class // is hiding the member variable "firstname" // of the parent class Person // // note! firstname is also present // in the Person class and is inherited // in this Employee class String firstname = "Default firstname"; // specific member variables String employeeid; int payscale; String joiningDate; // constructor Employee(String firstname, String lastname, String employeeid, int payscale, String joiningDate) { // calling the constructor of the parent class Person // to set the first name and last name super(firstname, lastname); // now setting the member variables of this class this.employeeid = employeeid; this.payscale = payscale; this.joiningDate = joiningDate; } // show employee details public void showDetail() { System.out.println("Employee details:"); // this will give us the value stored in the // firstname member variable of this class System.out.println("First name (variable of Employee class): " + this.firstname); // to get the firstname that was saved in the // parent class member variable // we have to use the super keyword System.out.println("First name (variable of Person class): " + super.firstname); // showing the last name // this was inherited from the parent class Person // but since it is not hidden in the child class // Employee so, we are using the "this" keyword // to access it System.out.println("Last name (inherited variable of Employee class): " + this.lastname); // we can also use the "super" keyword and // access the lastname variable of the Person class System.out.println("Last name (variable of Person class): " + super.lastname); // now calling the method of this class this.showEmployeeDetail(); } public void showEmployeeDetail() { System.out.println("Employee ID: " + this.employeeid); System.out.println("Pay scale: " + this.payscale); System.out.println("Joining Date: " + this.joiningDate); } } // the main class public class Example { // the main method public static void main(String[] args) { // employee data String firstname = "Yusuf"; String lastname = "Shakeel"; String employeeid = "E01"; int payscale = 3; String joiningDate = "2010-01-01"; // creating an object of the Employee class Employee empObj = new Employee(firstname, lastname, employeeid, payscale, joiningDate); // show detail empObj.showDetail(); } }
$ javac Example.java $ java Example Employee details: First name (variable of Employee class): Default firstname First name (variable of Person class): Yusuf Last name (inherited variable of Employee class): Shakeel Last name (variable of Person class): Shakeel Employee ID: E01 Pay scale: 3 Joining Date: 2010-01-01