The super keyword
The super keyword in the Java programming language is a user-defined declared class reference variable. The super keyword in Java classes is used to indicate the immediate parent class behavior of an existing class object. The super keyword helps in inheriting the data type method references of the class declared in the root class.

The super keyword is commonly used in Java in three ways.
- Accessing a parent class constructor.
- Accessing a parent class method.
- Accessing a parent class field.
Using super to access the parent class constructor in Java.
In a Java class program, when a subclass inherits a superclass method or property, the subclass can access the declared constructor reference of its parent class by applying the super() class keyword. If the root or parent class has declared or defined a parameterized constructor. So, by applying the super() keyword to the subclass declared in the root class, we can directly call the parent constructor with the proper class parameter arguments.
Example of using super to access the parent class constructor.
class Course {
String course_name;
// Here we declare a parent course class constructor
public Course(String course_name) {
this.course_name = course_name;
System.out.println(“here the Course class constructor was called”);
}
public void select_course() {
System.out.println(course_name + ” you can select the desired course”);
}
}
class Java extends Course {
// Here we create a Java subclass with the super keyword constructor
public Java(String course_name) {
super(course_name); // Here it is calling the parent course class constructor
System.out.println(“Java constructor called”);
}
@Override
public void select_course() {
System.out.println(course_name + ” Java programming selected”);
}
}
public class Main {
public static void main(String[] args) {
Java java = new Java(“Siddhi,”);
java.select_course();
}
}
Explanation of using super to access the parent class constructor.
- Here, a constructor is defined in the Course class, which initializes the course_name field value in the Course class.
- The Java subclass defined in the root class calls the Course class’s constructor using super(course_name) to initialize the course_name field.
- The super keyword in the super(course_name) method is important here because the Course class does not define a no-argument constructor. A parameterized constructor is defined here, so we need to explicitly call it directly. This overrides the select_course() method of the Java class.
Use the super keyword to access a parent class method in Java.
The super keyword in the Java root or main class is used to call a method of the parent class. Specifically, if a user-defined method is overridden in a subclass and you want to invoke the superclass version of the Java user method.
Example of the super keyword to access a parent class method.
class Course {
public void select_course() {
System.out.println(“Select your Course”);
}
}
class Java extends Course {
@Override
public void select_course() {
super.select_course(); // Calling the select_course() method of the parent class (Course)
System.out.println(“Java language selected”);
}
}
public class Main {
public static void main(String[] args) {
Java java = new Java();
java.select_course();
}
}
Explanation of the super keyword to access a parent class method.
- Here, the Java subclass overrides the select_course() method. Inside the overridden method in the Java subclass, the super.select_course() function is used to call the select_course() method from the Course class.
- Here, we maintain the behavior of the superclass method while adding new functionality to the Java subclass as needed.
Using super to access parent class fields in Java.
If a field in a Java subclass is defined or declared with the same name as a field in the parent class, the super keyword can be used to access the class parameter field from the parent class, hiding the variable in the root class in the current program.
Example of super to access parent class fields.
class Course {
String course_name = “Course”;
public void course_info() {
System.out.println(“This is a course info”);
}
}
class Java extends Course {
String course_name = “Java”;
public void course_info() {
super.course_info(); // here it is calling the parent’s course_info method
System.out.println(“This is a Java language”);
System.out.println(“Parent class course_name is – ” + super.course_name); // Here it’s accessing the parent’s field using the ‘super’ keyword
System.out.println(“Current class course_name is – ” + course_name); // Here it’s accessing the current class’s field
}
}
public class Main {
public static void main(String[] args) {
Java java = new Java();
java.course_info();
}
}
Explaining how to access parent class fields.
- In this example, both the Course class and the Java class have a field named course_name defined.
- In the Java class, we use super.name to reference the course_name field in the Course class.
- We also use the course_name variable directly to reference the course_name field in the Java class.
