How to Use the super Keyword to Call a Base Class Constructor in Java
See Java: Tips and Tricks for similar articles.
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 understand how to use the super keyword to call a base class constructor, follow these 10 steps.
- Open your text editor and type in the following Java statements:
The program defines two properties, firstNameandlastName. 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. - Save your file as Person.java.
- 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.

- Create a new file in your text editor. Type in the following Java statements:
The Employeeclass 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. - Save your file as Employee.java.
- Return to the command prompt. Then type in the command to compile
the source and hit Enter.

- 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:
The program instantiates an Employee object and then calls the getFirstNameandgetLastNamemethods. The first name and last name parameters have been passed to the base constructor by the derived class constructor. The program also calls thegetEmpIdmethod in the Employee object. - Save your file as UsesuperKeywordToCallBaseConstructor.java.
- Return to the command prompt. Then type in the command to compile
the source and hit Enter.

- 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.
