Java: super keyword

In this article, we will discuss super keyword in Java

super keyword in java is used to refer immediate parent class

 

Usage of super keyword in Java

  • Instance variable: super keyword is used to refer instance variables of immediate parent class
  • Super constructor: super() constructor call; is used to invoke constructor of immediate parent class
  • Overridden Method: <methodName> is used to invoke instance method of immediate parent class

Note: super cannot be used refer anything in static context (Cannot use super in a static context)

 

Let us understand each one in detail with examples

 

Usage 1: super keyword to access variables of immediate parent class

Syntax: super.<variable_name>

 

Case 1: Variable Hiding

Variable Hiding: If instance variable name of both child class and parent class are same, then child class instance variable hides the parent class’s instance variable

Let us take a close look at the need of super in inheritance concept

ParentClass.java

package in.bench.resources.superkeyword.example;

public class ParentClass {

	// instance variables
	int siteAge = 20;
	String siteName = "oracle.com";

	// other things
}

ChildClass.java

package in.bench.resources.superkeyword.example;

public class ChildClass extends ParentClass {

	// instance variables
	int siteAge = 2;
	String siteName = "BenchResources.Net";

	// display() method to print website details
	void displaySiteDetail() {
		System.out.println("Website age in years : " + siteAge);
		System.out.println("Website name : " + siteName);
	}

	// main() method - entry point to JVM to begin execution
	public static void main(String[] args) {

		ChildClass child = new ChildClass();
		child.displaySiteDetail();
	}
}

Output:

Website age in years : 2
Website name : BenchResources.Net

 

Explanation:

In above example,

  • There are 2 instance variables viz.; siteAge and siteName in both parent and child classes
  • When we accessed these instance variables from display() method, it prints current class’s values
  • Reason: current class’s instance variables hides parent class’s instance variables which are having same name

 

What if we need to access parent class’s instance variables?

By use of super keyword, we can invoke immediate parent class’s instance variables

 

Case 2: Accessing parent class variables using super keyword

Let us look at the modified code to access parent class’s instance variables

ParentClass.java

package in.bench.resources.superkeyword.example;

public class ParentClass {

	// instance variables
	int siteAge = 20;
	String siteName = "oracle.com";

	// other things
}

ChildClass.java

package in.bench.resources.superkeyword.example;

public class ChildClass extends ParentClass {

	// instance variables
	int siteAge = 2;
	String siteName = "BenchResources.Net";

	// display() method to print website details
	void displaySiteDetail() {
		System.out.println("Website age in years : " + super.siteAge);
		System.out.println("Website name : " + super.siteName);
	}

	// main() method - entry point to JVM to begin execution
	public static void main(String[] args) {

		ChildClass child = new ChildClass();
		child.displaySiteDetail();
	}
}

Output:

Website age in years : 20
Website name : oracle.com

 

Explanation:

In above example,

  • Using super keyword, we can invoke immediate parent class’s variables
  • siteAge accesses the parent class’s age instance variable
  • similarly, siteName accesses the parent class’s name instance variable

Note: If the instance variable names of child and parent class is different then there is no need of super keyword instead we can directly access

 

 

Usage 2: Invoke super() constructor of immediate parent class

Syntax: super() constructor call; for invoking default constructor of immediate parent class

Syntax: super(…) constructor call; `for invoking parametrized constructor of immediate parent class

 

Case 1: Implicit super() constructor call inserted by compiler during compilation

ParentClass.java

package in.bench.resources.superkeyword.example;

public class ParentClass {

	// instance variables
	int siteAge = 20;
	String siteName = "oracle.com";

	//	default no-arg constructor 
	ParentClass(){
		System.out.println("default constructor of Parent class");
	}

	// other codes here
}

ChildClass.java

package in.bench.resources.superkeyword.example;

public class ChildClass extends ParentClass {

	// instance variables
	int siteAge = 2;
	String siteName = "BenchResources.Net";

	//	default no-arg constructor 
	ChildClass() {
		System.out.println("default constructor of Child class");
	}

	// display() method to print website details
	void displaySiteDetail() {
		System.out.println("Website age in years : " + super.siteAge);
		System.out.println("Website name : " + super.siteName);
	}

	// main() method - entry point to JVM to begin execution
	public static void main(String[] args) {

		ChildClass child = new ChildClass();
		//		child.displaySiteDetail();
	}
}

Output:

default constructor of Parent class
default constructor of Child class

 

Explanation:

In above example,

  • there is no explicit super() constructor call
  • but still parent class’s constructor is invoked
  • Because, compiler during compilation inserts super() constructor call as the 1st statement in all constructor
  • Due to which parent class’s constructor is invoked as you can see from the above output

 

Case 2: Explicit super() constructor call inserted by programmer

ParentClass.java

package in.bench.resources.superkeyword.example;

public class ParentClass {

	// instance variables
	int siteAge = 20;
	String siteName = "oracle.com";

	//	default no-arg constructor 
	ParentClass(){
		// here super() will be inserted; 
		// which will invoke java.lang.Object class's default constructor
		System.out.println("default constructor of Parent class");
	}

	// other codes here
}

ChildClass.java

package in.bench.resources.superkeyword.example;

public class ChildClass extends ParentClass {

	// instance variables
	int siteAge = 2;
	String siteName = "BenchResources.Net";

	//	default no-arg constructor 
	ChildClass() {
		super(); // explicitly inserted, it should be 1st statement of constructor
		System.out.println("default constructor of Child class");
	}

	// display() method to print website details
	void displaySiteDetail() {
		System.out.println("Website age in years : " + super.siteAge);
		System.out.println("Website name : " + super.siteName);
	}

	// main() method - entry point to JVM to begin execution
	public static void main(String[] args) {

		ChildClass child = new ChildClass();
		//		child.displaySiteDetail();
	}
}

Output:

default constructor of Parent class
default constructor of Child class

 

Explanation:

In above example,

  • there is an explicit super() constructor call inserted by programmer
  • So, it invokes the parent class’s default no-arg constructor and output remain same as that of earlier one
  • Note: Make sure while inserting super() constructor call explicitly; it must be 1st statement of the constructor;
  • Otherwise compilation error will be thrown stating reason “Constructor call must be the first statement in a constructor

 

There is no point in explicitly invoking super() constructor call, when we know compiler inserts super call implicitly during compilation

 

So, when it is required?

To invoke immediate parent class’s parametrized constructor using super(arguments);

 

Case 3: Explicit super() constructor call inserted by programmer

ParentClass.java

package in.bench.resources.superkeyword.example;

public class ParentClass {

	// instance variables
	int siteAge;
	String siteName;

	//	2-arg parametrized constructor 
	ParentClass(int siteAge, String siteName){
		// here super() will be inserted; 
		// which will invoke java.lang.Object class's default constructor
		System.out.println("2-arg parametrized constructor of Parent class");

		// assigning instance variables of this class
		this.siteAge = siteAge;
		this.siteName = siteName;
	}

	// other codes here
}

ChildClass.java

package in.bench.resources.superkeyword.example;

public class ChildClass extends ParentClass {

	// instance variables
	int siteAge = 2;
	String siteName = "BenchResources.Net";

	//	default no-arg constructor 
	ChildClass() {
		// explicitly inserted to invoke 2-arg parametrized constructor
		// it should be 1st statement of constructor
		super(20, "oracle.com"); 
		System.out.println("default constructor of Child class");
	}

	// display() method to print website details
	void displaySiteDetail() {
		System.out.println("\nWebsite age in years : " + super.siteAge);
		System.out.println("Website name : " + super.siteName);
	}

	// main() method - entry point to JVM to begin execution
	public static void main(String[] args) {

		ChildClass child = new ChildClass();
		child.displaySiteDetail();
	}
}

Output:

2-arg parametrized constructor of Parent class
default constructor of Child class

Website age in years : 20
Website name : oracle.com

 

Explanation:

In above example,

  • there is an explicit super(int, String) constructor call inserted by programmer
  • So, it invokes the parent class’s 2-arg parametrized constructor; while creating object of child class through inheritance relationship
  • Note: Make sure while inserting super(arguments) constructor call explicitly; it must be 1st statement of the constructor;
  • Otherwise compilation error will be thrown stating reason “Constructor call must be the first statement in a constructor

 

Usage 3: super keyword to invoke instance method of immediate parent class

Syntax: super.<overridden_instance_method>

Both parent class and child class have method with same method name i.e.; child class overriding parent class’s instance method

 

Case 1: Without super keyword

When child class overrides parent class’s method, then if we invoke directly without any keyword it calls current class method i.e.; overriding method

Let us see an example

ParentClass.java

package in.bench.resources.superkeyword.example;

public class ParentClass {

	// instance variables
	int siteAge;
	String siteName;

	// overridden display() method
	void displayMessage() {
		System.out.println("Parent class instance method displayMessage()");
	}
}

ChildClass.java

package in.bench.resources.superkeyword.example;

public class ChildClass extends ParentClass {

	// instance variables
	int siteAge = 2;
	String siteName = "BenchResources.Net";

	// overriding display() method
	void displayMessage() {
		System.out.println("Child class instance method displayMessage()");
	}

	// test method for super keyword
	void callDsiplayTest() {
		// invokes overriding method
		displayMessage(); // without super keyword
	}

	// main() method - entry point to JVM to begin execution
	public static void main(String[] args) {

		ChildClass child = new ChildClass();
		child.callDsiplayTest();
	}
}

Output:

Parent class instance method displayMessage()

 

Explanation:

In above example,

  • Directly invoking instance method from child class –> calls overriding method
  • Because priority is give to local method
  • To invoke overridden method of parent class from child class –> we need to explicitly invoke using super keyword

Let us see an example on the use of super keyword for overridden method invocation

 

Case 2: Invoking parent class’s method using super keyword

Invoking parent class’s overridden method using super keyword

ParentClass.java

package in.bench.resources.superkeyword.example;

public class ParentClass {

	// instance variables
	int siteAge;
	String siteName;

	// overridden display() method
	void displayMessage() {
		System.out.println("Parent class instance method displayMessage()");
	}
}

ChildClass.java

package in.bench.resources.superkeyword.example;

public class ChildClass extends ParentClass {

	// instance variables
	int siteAge = 2;
	String siteName = "BenchResources.Net";

	// overriding display() method
	void displayMessage() {
		System.out.println("Child class instance method displayMessage()");
	}

	// test method for super keyword
	void callDsiplayTest() {
		// invokes overriding method
		super.displayMessage(); // invoking parent class method using super keyword
	}

	// main() method - entry point to JVM to begin execution
	public static void main(String[] args) {

		ChildClass child = new ChildClass();
		child.callDsiplayTest();
	}
}

Output:

Parent class instance method displayMessage()

 

Explanation:

In above example,

  • We are explicitly invoking parent class method using super
  • Reason: Local instance method hides parent class overridden method while invoking without super keyword, as seen in the earlier case

Note: If instance method names are different in parent class and child class then there is no need of super keyword to invoke parent class; instead directly parent class’s instance method can be invoked through inheritance

 

Point to remember about super keyword:

  • super keyword is used to refer immediate parent class
  • super.<variable_name> is used to refer immediate parent class’s instance variable
  • super() constructor call; is used to invoke immediate parent class’s default no-arg constructor
  • super(arguments) constructor call; is used to invoke immediate parent class’s parametrized constructor
  • super.<overridden_method> is used to invoke immediate parent class’s instance method

That’s all about super keyword in Java

 

References:

https://docs.oracle.com/javase/tutorial/java/IandI/super.html

 

Read Also:

Happy Coding !!
Happy Learning !!