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-variable of immediate parent-class (super-class)
- Super constructor: super() constructor call; is used to invoke constructor of immediate parent-class (super-class)
- Overridden Method: <methodName> is used to invoke instance method of immediate parent-class (super-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 (super-class)
- Syntax: super.<variable_name>
Case 1.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’ 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:
- There are 2 instance-variables viz.; siteAge and siteName in both parent/child classes
- When we access these instance-variables from display() method, then it prints current class’ values of same variable-name (instead of immediate super-class)
- Reason: current class’ instance-variables hides parent-class’s instance-variable which are having same variable-name
1.1.Q. What if we need to access parent class’ instance-variables ?
- By use of super keyword, we can invoke/call/refer immediate parent-class’ instance-variable
Case 1.2. Accessing parent-class variables using super keyword
Let us look at the modified code to access parent-class’ instance-variable
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:
- Using super keyword, we can invoke immediate parent-class’ variable
- siteAge accesses parent-class’ site-age instance-variable
- similarly, siteName accesses parent-class’ site-name instance-variable
- Note: If the instance-variable names of child-class and parent-class is different, then there is no need of super keyword; instead we can directly access it
Usage 2: Invoke super() constructor of immediate parent class
- Syntax: super() constructor call; for invoking default constructor of immediate parent-class
- Syntax: super(args) constructor call; for invoking parameterized constructor of immediate parent-class
Case 2.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:
- there is no explicit super() constructor call
- but still parent-class’ constructor is invoked
- Because, compiler during compilation inserts super() constructor call, as the 1st statement in all constructor
- Due to which parent-class’ constructor is invoked as you can see from the above output
Case 2.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 Object class' 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() { // explicitly inserted super(); // it should be 1st statement of constructor System.out.println("default constructor of Child class"); } // display() method to print web-site 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:
- there is an explicit super() constructor call inserted by programmer
- So, it invokes the parent-class’ default no-arg constructor and output remain same as that of previous example
- 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”
- Note :- There is no point in explicitly invoking super() constructor call, when we know compiler inserts super call implicitly during compilation
2.2.Q. So, when it is required ?
- To invoke immediate parent-class’ parameterized-constructor using super(arguments);
Case 2.3. Explicit super() constructor call inserted by programmer
ParentClass.java
package in.bench.resources.superkeyword.example; public class ParentClass { // instance-variable int siteAge; String siteName; // 2-arg parameterized-constructor ParentClass(int siteAge, String siteName) { // here super() will be inserted; // which will invoke Object class' default-constructor System.out.println("2-arg parameterized-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 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 parameterized constructor of Parent class default constructor of Child class Website age in years : 20 Website name : oracle.com
Explanation:
- there is an explicit super(int, String) constructor call inserted by programmer
- So, it invokes the parent-class’ 2-arg parameterized-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’ instance-method
Case 3.1. Without super keyword
- When child-class overrides parent-class’ instance-method
- and if we invoke method directly without any keyword, then 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:
Child-class instance-method displayMessage()
Explanation:
- Directly invoking instance-method from child-class –> calls overriding-method
- Because priority is given 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 3.2. Invoking parent-class’ method using super keyword
- Invoking parent-class’ 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 } // 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:
- We are explicitly invoking parent-class method using super keyword
- Reason: Local instance-method hides parent-class overridden-method while invoking without super keyword, as seen in the earlier case 3.1
- 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 methods; instead directly parent-class’ 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’ instance-variable
- super() constructor call; is used to invoke immediate parent-class’ default no-arg constructor
- super(arguments) constructor call; is used to invoke immediate parent-class’ parameterized-constructor
- super.<overridden_method> is used to invoke immediate parent-class’ instance-method
That’s all about super keyword in Java
References:
Read Also:
- instanceof keyword
- final keyword
- static keyword
- this keyword
- Interview question and answers on final keyword
Happy Coding !!
Happy Learning !!