Register Login

Super Keyword in Java

Updated Mar 19, 2021

As we all know, Java is rich in keywords and is a pure object-oriented programming language; in this article, we will get to know about the keyword Super. If you are not familiar with Child class and Parent class concepts, check out the previous tutorials.

Super Keyword - Definition and Usage

In Java, the super keyword refers to an object, that is, a reference variable of an immediate parent class object. Whenever you create the instance of a subclass, an instance of your parent class gets implicitly created. The super keyword helps in referencing such variables. The super keyword helps in referencing such variables. Here are some of the common usages of the super keyword.

  • It acts as a caller of the superclass method.
  • The super keyword helps in accessing the superclass constructor.
  • Programmers also use the super keyword because both the super-classes and subclasses having the same method name. To call the method of the parent class from within the child class avoiding the same method name from the child class, Java programmers use the super keyword. That is when the child class overrides the method already created in the parent class.

To have a practical understanding of the super keyword, you need to have the prerequisite knowledge of Polymorphism and Inheritance

Example

class Engineer { 		// Superclass (parent class)
  public void coding() 
  {
    	System.out.println("Computer Engineers are Programmers");
  }
}

class Coder extends Engineer { 		// Subclass (child class)
  public void coding()
  {
    super.coding(); 	// Call the superclass method
    System.out.println("Coders also know different Programming languages");
  }
}

public class Main {
  public static void main(String args[]) {
    Engineer e1 = new Coder(); 	// Creates a Coder object
    e1.coding(); 	// Call the method on the Coder object
  }
}

In this program, we have the method name coding() in both the parent class as well as the child class. From the Main class, we have created an object e1 of the child class that invokes the coding() method. It calls the coding() method of the child class in which the super.coding() is responsible for calling the coding() method of the parent class.

Note that the super keyword statement should be the first statement of the derived class constructor.

Access superclass (parent class) variable

Programmers can also use the super keyword to access the data member (variables within a class) of the parent class. The super keyword becomes handy when both the parent and the child class have the same variable name.

Example

class Engineer { 		// Superclass (parent class)
    	String name = "Karlos";
}
class Coder extends Engineer 
{
// Subclass (child class)
     void printName() {  
        String name = "Ray"; 
    System.out.println(name);     
    System.out.println(super.name);
    }
}

public class Main {
  public static void main(String args[]) {
    Coder e1 = new Coder(); 	
    e1.printName(); 
  }
}


In this program, we have put two string-type variables with the same name. One 'name' variable in the parent class and the other in the child class but with different values assigned to each one of them. Now, from the child class method, if you want to access the variable of the parent class by bypassing the same variable name, we've to use the super keyword as shown System.out.println(super.name);. To access the child class variable we have simply use System.out.println(name);.

Access superclass method

We use the super keyword when there are two methods (in the parent class and the child class) with the same name. The super keyword comes to the rescue when programmers want to call the method of the parent class from within the child class, avoiding the same method name from the child class. Here is a code snippet showing the use of super with this functionality.

Example

class Engineer { 		// Superclass (parent class)
    	void message()
    	{
    	    System.out.println("This is parent class");
    	}
}

class Coder extends Engineer 
{
void message()
    	{
    	    System.out.println("This is child class");
    	}
    	void display() 
   	 { 
        // this method call will invoke message() method of the current class
        message(); 
  
        // this method call will invoke message() method of the parent class
        super.message(); 
    } 
}

public class Main {
  public static void main(String args[]) {
    Coder e1 = new Coder(); 	
    e1.display(); 
  }
}

Here also, we have created an object e1 of the child class that invokes the message() method. It calls the message() method of the child class within which the super.message() is responsible for calling the message() method of the parent class.

The process of calling the parent class method ignoring the child class method is called method overriding.

Access Constructor of a Superclass

Programmers also utilize the super keyword to access the constructor of the parent class. Also, the super keyword can call both parameterized and non-parameterized constructors based on the constructor type and code. Here is a code snippet showing its use.

Example

class Engineer { 	// Superclass (parent class)
    	Engineer()
    	{
    	    System.out.println("This is a parent class constructor");
    	}
}

class Coder extends Engineer 
{
    Coder()
{
    	    super();    // this will invoke the constructor of the parent class
    	    System.out.println("This is a child class constructor");
    	}
}

public class Main {
  public static void main(String args[]) {
    Coder e1 = new Coder(); 	
  }
}

Here also, we have two separate constructors of the two respective classes. Also, we have created an object e1 of the child class that invokes the Coder class constructor automatically when the object gets created. The Coder() constructor then invokes the super(), which helps in invoking the constructor of the parent class from within the child class.

What if the child class is not overriding the method of parent class?

There are certain situations when the child class does not override the method of its parent class. In that case, we do not need to use the super keyword with the dummy child class method. In such a situation, we have only one version of the method. As per the rule of inheritance, the child class can access the parent class method. Therefore, we can directly invoke the parent class method without using the super keyword.

Example

class Engineer { 	// Superclass (parent class)
    	void disp()
    	{
    	    System.out.println("This is a parent class");
    	}
}

class Coder extends Engineer 
{
        void Printt()
        {
    	    disp();    
    	}
}

public class Main {
  public static void main(String args[]) {
    Coder e1 = new Coder(); 	
    e1.Printt();
  }
}

In this program, we have not used the super keyword because, as per the inheritance rule, the child class can access the parent class method that our disp() method did. Calling the disp() from Printt() within the child class simply called the disp() method of the parent class.


×