Super Keyword in Java

In this session, we will understand the important concept of Java which is important for inheritance implementation is super Keyword in Java.

Objectives:

  • What is super Keyword in Java?
  • Calling Constructor in Inheritance
  • Use of Super Keyword
  • Calling constructor by using super keyword
  • Difference Between this and super keyword

What is super Keyword in Java?

The super is nothing but a keyword in java. By using the super keyword we can access the member of the super class. If you want to more about super class, you can navigate to Inheritance to understand more.

The super keyword is using inside the sub class for accessing the instance variable of super class only if the instance variable name is same for both sub class and super class. Let’s understand the usage of super keyword with a simple example.

Example:

class X {
	int val=9;
}
class Y extends X {
	int val=89;
	void showSuper() {
		System.out.println("Child class variable : " + val);
		System.out.println("Super class variable through super keyword : " +super.val);
	}
}
public class DemoSuper {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Y a = new Y();
		a.showSuper();
	}

}

Output:

Child class variable : 89
Super class variable through super keyword : 9

Explanation:

From the above example, you can notice that we have declared the same variable name in both child and parent class and assigned different value. When we are trying to print the value of variable directly through child class object, then it is printing 89 which is from the child class variable. But in the next statement when you are trying to print the same variable though the child class object but with a super keyword, then it will point to the parent class / super class and print 9 which is the parent class variable.

Same as variable we can do same for the method also. If the same method name is declared in both of the child and parent class then we can call the parent class method with super keyword.

Note: Java uses the super keyword for explicitly calling to the parent class constructor within the child class constructor.

Example:

class X {
	 
	X() {
		System.out.println("Parent class method of name show");
	}
}
class Y extends X {
	 
	
	Y() {
		super(); // Call the super class constructor
		System.out.println("Child class method of name show");

	}
	
}
public class DemoSuper {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Y d = new Y();
		
		
	}

}

Output:

Parent class method of name show
Child class method of name show

Explanation:

You can see the above code flow. When we are creating the object of the child class, it is calling the child class constructor. When the first statement of the child class constructor will be executed which is super(), it will point to the super class constructor. That is why parent class constructor is executed. After done, it will move the print statement of the child class.

There is one restriction with declaring super(). The super() must be the first statement of the child class constructor. Otherwise it will through the compile time error. Let’s have a look on below code.

Example:

class X {
	 
	X() {
		System.out.println("Parent class method of name show");
	}
}
class Y extends X {
	 
	
	Y() {
		super(); // Call the super class constructor
		System.out.println("Child class method of name show");

	}
	
}
public class DemoSuper {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Y d = new Y();
		
		
	}

}

Output:

Explanation:

From the above example, we can notice that if we will not declare the super() in the first statement of the child class constructor then it will throw an  Unresolved compilation problem.

Note: Like super, this keyword should also be the first statement of the constructor. this is the keyword which is used to point to the current class constructor. We will discuss more on this keyword later. 

How to call Constructor in Inheritance?

In Inheritance, when we create the child class object by calling the child class constructor with new keyword, it will invoke implicitly the super class constructor. If you do not have the default constructor defined in the super class, then it will throw an error. Let’s understand this with an Example.

Example:

class X {
	 
	X() {
		System.out.println("Parent class method of Default Constructor");
	}
	X(int a) {
		System.out.println("Parent class method of Parameterized Constructor");
	}
}
class Y extends X {
	 
}
 
public class ConstructorCallDefault {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Y d = new Y();
	}
}

Output:

Parent class method of Default Constructor

Explanation:

From the above example, you can notice that, X is Parent class and Y is child class. And we have created object of child class named d. But we have not called any of method of X class. Still it is printing “Parent class method of Default Constructor” because this is child class object calls the parent class default constructor implicitly.

What if we will create the child class object throw new keyword without any default constructor in parent class? 

Let’s have a look on that.

Example

class X {
	X(int a) {
		System.out.println("Parent class method of Parameterized Constructor");
	}
}
class Y extends X {

 	 
}
 
public class NoDefaultConstuctor {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Y d = new Y();
	}
}

Output:

Explanation:

From the above example, you can notice that, X is Parent class and Y is child class. And we have created object of child class named d. But we have not declared any default constructor in child class. Due to this it is throwing the compile time error.

Example:

class X {
	 
	X() {
		System.out.println("Parent class method of Default Constructor");
	}
	X(int a) {
		System.out.println("Parent class method of Parameterized Constructor");
	}
}
class Y extends X {

	public Y(int i) {
		System.out.println("Child class method of Parameterized Constructor");
		}
	 
}
 
public class Demo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Y d = new Y(5);
	}
}

Output:

Parent class method of Default Constructor
Child class method of Parameterized Constructor

Explanation:

From the above example, you can notice that, X is Parent class and Y is child class. And we have created object of child class named with parameterized constructor. Still it is invoking the default constructor implicitly.

Let’s think another scenario where we do not have default constructor in child class. We will define the parameterized child class constructor only. In that case how to call the super class parameterized constructor?

Let’s understand with an Example.

Example:

class X {
	 

	X(int a) {
		System.out.println("Parent class method of Parameterized Constructor");
	}
}
class Y extends X {

	public Y(int i) {
		super(i);
		System.out.println("Child class method of Parameterized Constructor");
		}
	 
}
 
public class DemoConstructor {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Y d = new Y(5);
	}
}

Output:

Parent class method of Parameterized Constructor
Child class method of Parameterized Constructor

Explanation:

From the above example, you can notice that, X is Parent class and Y is child class. And we have created object of child class named with parameterized constructor. To call the super class parameterized constructor we need to define the parameterized constructor inside the child class and in the first statement, we need to define the super() keyword with a parameter.

super(i);

Difference Between super and this Keyword.

  • super and this both are the keyword in java. We can not use super and this as identifier in java. If we try to use, it will throw an Error.
  • If you want to refer the instance variable of the current class or to invoke the constructor of the current class, then “this” keyword can be used.
  • Addition to refer the current class, if you want to return the current class instance, then you need to use “this´ keyword.
  • this keyword is used to pass an argument in both Method call and Constructor call.
  • “super” keyword is generally used in Inheritance. In Inheritance you can refer the super class instance, method, or members through this “super” keyword.
  • Addition to refer, “super” keyword can be used to invoke the super class instance, method, or members.
  • When you want to invoke the superclass version of overridden method, then you need to use super keyword. Whereas when you want to invoke the current class version of overridden method, then you need to use this keyword.

Summary:

Let’s summarized the usage of super keyword in java.

  • It can be used to refer immediate parent class Instance Variable
  • super can be used to refer immediate parent class Method
  • It can be used to refer immediate parent class Constructor
  • super() should be the first statement in the class Constructor.
  • Like super, this keyword should also be the first statement of the constructor. this is the keyword which is used to point to the current class constructor. We will discuss more on this keyword later. 

Leave a Reply

Your email address will not be published. Required fields are marked *