We discussed Constructor in Java in our previous tutorial and we understood that the lifespan of any constructor depends upon the lifespan of the class. The topics which we are going to discuss today are again related to Java class and they are efficiently used when we are implementing inheritance in our Java project. Thus, we are going to discuss this and super keyword in Java today. They are very significant for the OOPS programming approach.
Let’s understand them.
Understanding Java’s this keyword
When we require giving the reference variable of the current class then we use this keyword. Let’s quickly go through the different uses of this keyword in Java.
- this keyword is used to implicitly invoke the current class method
- this() keyword invoke the constructor of the current class
- this keyword is used to refer to the instance variable of the current class
- this keyword is passed as an argument in method call when the parameter is the current class
- this keyword is used to return the instance of the current class from the method
- Similar to the method’s argument, this keyword is also passed as an argument in the constructor call
Let’s look at the usage of the above points through sample programs.
Invoke current class method using this keyword in Java
package my.project; public class Testing { public void method1(){ System.out.println("This is method 1"); } public void method2(){ System.out.println("This is method 2"); this.method1(); } public static void main(String args[]){ Testing obj =new Testing(); obj.method2(); } }
Invoke constructor of the current class using this keyword in Java
package my.project; public class Testing { Testing(){ System.out.println("This is Default Constructor"); } Testing(int val){ this(); System.out.println("This is Parameterized Constructor: "+val); } public static void main(String args[]){ Testing obj =new Testing(5); } }
Call to constructor will always be the first statement.
Remove the ambiguity of instance variable by using this keyword in Java
package my.project; public class Testing { int rollNo; String name; String ssn; Testing(int rollNo, String name, String ssn){ this.rollNo=rollNo; this.name=name; this.ssn=ssn; } }
Pass this keyword as an argument in Methods of Java
package my.project; public class Testing { public void method(Testing obj){ System.out.println("Method invoked. Instance of: "+obj.getClass().getSimpleName()); } public void anotherMethod(){ System.out.println("Invoking..."); this.method(this); } public static void main(String[] argv){ Testing obj = new Testing(); obj.anotherMethod(); } }
Return instance of the current class using this keyword
package my.project; public class Testing { public Testing method(){ System.out.println("Returning Current Instance"); return this; } public void anotherMethod(){ System.out.println("Invoking...Another Method"); } public static void main(String[] argv){ new Testing().method().anotherMethod(); } }
Pass this keyword as an argument in Constructor of Java
package my.project; public class Testing{ AnotherTesting obj; Testing(AnotherTesting obj){ this.obj=obj; } void displaymethod(){ System.out.println(obj.data); } public static void main(String args[]){ AnotherTesting aObj=new AnotherTesting(); } } class AnotherTesting{ int data=10; AnotherTesting(){ Testing bObj=new Testing(this); bObj.displaymethod(); } }
Understanding Java’s super keyword
We saw above this keyword is used to refer to the current class reference variable; similarly, a super keyword is used to refer to the immediate parent class reference variable from the subclass. It works on the principle that an instance of the parent class gets created implicitly whenever we create an instance of the subclass.
Let’s understand the usage of the super keyword in Java
- the super keyword is used to give the reference to the instance variables of the parent class
- the super keyword is used to invoke the method of the immediate parents class
- the super() keyword is used to invoke the constructor of the immediate parent class
Let’s have a look at the sample programs of the above-mentioned statements in bullets:
Refer to instance variables of the immediate parent class using super keyword
package my.project; public class Animal{ String color = "white"; public static void main(String[] argv){ Cow obj = new Cow(); obj.displayColor(); } } class Cow extends Animal{ String color = "black"; public void displayColor(){ System.out.println("Color in Animal Class: "+super.color); System.out.println("Color in Cow Class: "+this.color); } }
Invoke methods of the immediate parent class using super keyword
package my.project; public class Animal{ public void animalEating(){ System.out.println("Animal is eating..."); } public static void main(String[] argv){ Cow obj = new Cow(); obj.invokeEating(); } } class Cow extends Animal{ public void invokeEating(){ super.animalEating(); } }
Invoke constructor of the immediate parent class using super keyword
package my.project; public class Animal{ Animal(){ System.out.println("Invoking Parent Class Constructor"); } public static void main(String[] argv){ Cow obj = new Cow(); } } class Cow extends Animal{ Cow(){ super(); } }
This was all about this and super keyword in Java. Feel free to post your queries using the below comment form. See you in the next tutorial on the static and final keyword. Do not forget to join our Facebook group for the latest updates on Selenium and Test automation engineering.
Hello
This blog post is informative one
thank you so much