How to Use the super Keyword to Call a Base Class Constructor in Java

  • google plus

In Brief...

A derived Java class can call a constructor in its base class using the super keyword. In fact, a constructor in the derived class must call the super's constructor unless default constructors are in place for both classes. To use the super keyword to call a base class constructor, follow these 10 steps.

Instructions

  1. Open your text editor and type in the following Java statements:
    Java Source for Super Class
    The program defines two properties: firstName and lastName. Notice that the program follows the Javabean specification for defining a property, i.e., a private variable with getter and setter methods. Also note that the constructor requires two parameters: the first name and the last name.

  2. Save your file as Person.java.

  3. Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter.
    Compile Source for Super Class

  4. Create a new file in your text editor. Type in the following Java statements:
    Java Source for Derived Class
    The Employee class defines one property: empId. This property will contain the employee ID. Notice that the constructor in this class calls the constructor in the super class (Person). The first name and last name parameters are passed to the constructor.

  5. Save your file as Employee.java.

  6. Return to the command prompt. Then type in the command to compile the source and hit Enter.
    Compile Source for Derived Class

  7. Now you will create an application to test your derived class. Create a new file in your text editor and type in the following Java statements:
    Java Source for Tester Class
    The program instantiates an Employee object and then calls the getFirstName and getLastName methods. The first name and last name parameters have been passed to the base constructor by the derived class constructor. The program also calls the getEmpId method in the Employee object.

  8. Save your file as UsesuperKeywordToCallBaseConstructor.java.

  9. Return to the command prompt. Then type in the command to compile the source and hit Enter.
    Compile Source for Tester Class

  10. You will now test your program. Type in the command to run the Java runtime launcher and then hit Enter. Notice the output of the program verifies that the first name and the last name were successfully passed to the base class constructor by the Employee constructor.
    Run Tester for Derived Class

Author: Stephen Withrow

Stephen has over 30 years' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.

Discuss