Introduction To Super And This Keyword In Java

Introduction

In this article, we discuss "super and this keyword in Java". super(), as well as this() both, are used to make constructor calls. super() is used to call Base class’s constructor(i.e, Parent’s class) while this() is used to call current class’s constructor.

Super-Keyword

Keywords are reserved words in Java, that have a predefined meaning to the compiler. The Super keyword indicates the following:

  • In our program when methods are overridden, then super is used to access the super-class methods, constructors and variables.
  • The super keyword in the Java programming language refers to the superclass of the class where the super keyword is currently being used.
  • Super is also used for a hidden field.
  • With the use of the keyword super, we can access a super-class member.

Super() is used to invoke:

  1. parent class instance variable.
  2. parent class constructor.
  3. parent class methods.

1. Refer to the parent class variable through super()

Example

Without the use of the super() keyword

  1. class Car  
  2. {  
  3.     int avgspeed = 250;  
  4. }  
  5.   
  6. class Bus extends Car  
  7. {  
  8.     int avgspeed=320;  
  9.     void print()  
  10.     {  
  11.         System.out.println(avgspeed);  
  12.     }  
  13.     public static void main(String args[])  
  14.     {  
  15.         Bus b = new Bus();  
  16.         b.print();  
  17.     }  
  18. }  
Output

fig-1.jpg

Now see the difference with the use of the super() keyword

  1. class Car  
  2. {  
  3.     int avgspeed = 250;  
  4. }  
  5.   
  6. class Bus extends Car  
  7. {  
  8.     int avgspeed=320;  
  9.     void print()  
  10.     {  
  11.         System.out.println(super.avgspeed);  
  12.     }  
  13.     public static void main(String args[])  
  14.     {  
  15.         Bus b = new Bus();  
  16.         b.print();  
  17.     }  
  18. }  
Output

fig-2.jpg

2. Invoke parent class constructor through super()

We can invoke or call a constructor from the parent class as in:

  1. class Car  
  2. {  
  3.     Car()  
  4.     {  
  5.         System.out.println("Car is launched");  
  6.     }  
  7. }  
  8.   
  9. class Bus extends Car  
  10. {  
  11.     Bus()  
  12.     {  
  13.         super();  
  14.         System.out.println("Bus is lauched");  
  15.     }  
  16.     public static void main(String args[])  
  17.     {  
  18.         Bus b = new Bus();  
  19.     }  
  20. }  
Output

fig-3.jpg

3. Invoke parent class method through super()

We can invoke a parent class method through the use of super() as in:

  1. class Parent  
  2. {  
  3.     void information()  
  4.     {  
  5.         System.out.println("Hello...!");  
  6.     }  
  7. }  
  8.   
  9. class Children extends Parent  
  10. {  
  11.    void information()  
  12.     {  
  13.         System.out.println("Hello world......!");  
  14.     }  
  15.     void print()  
  16.     {  
  17.         information();  
  18.         super.information();  
  19.     }  
  20.     public static void main(String args[])  
  21.     {  
  22.         Children c = new Children();  
  23.         c.print();  
  24.     }  
  25. }  
Output

fig-4.jpg

This-keyword in Java

In Java Programming "this" is a reference keyword used to refer to the current class method, contructor, etc.

How this() Keyword in Java is used?

1. Used to refer to the current class variable

Without the use of this() keyword

  1. class Children  
  2. {  
  3.     int stid;  
  4.     String stname;  
  5.     Children(int stid, String stname)  
  6.     {  
  7.         stid = stid;  
  8.         stname = stname;  
  9.     }  
  10.     void print()  
  11.     {  
  12.         System.out.println(stid + " " + stname);  
  13.     }  
  14.     public static void main(String args[])  
  15.     {  
  16.         Children st1 = new Children(12"John");  
  17.         Children st2 = new Children(22"Sam");  
  18.         st1.print();  
  19.         st2.print();  
  20.     }  
  21. }  

Output

fig-5.jpg

Now see the difference when the this() keyword is used. This keyword is used to differentiate between a local variable and an instance variable.

  1. class Children  
  2. {  
  3.     int stid;  
  4.     String stname;  
  5.     Children(int stid, String stname)  
  6.     {  
  7.         this.stid = stid;  
  8.         this.stname = stname;  
  9.     }  
  10.     void print()  
  11.     {  
  12.         System.out.println(stid + " " + stname);  
  13.     }  
  14.     public static void main(String args[])  
  15.     {  
  16.         Children st1 = new Children(12"John");  
  17.         Children st2 = new Children(22"Sam");  
  18.         st1.print();  
  19.         st2.print();  
  20.     }  
  21. }  
Output

fig-6.jpg

2. Using the this() keyword invokes the current class method.

If this keyword is not used in a program then the compiler automatically adds this keyword when using (invoking) the method.

  1. class InvokeMethodEx  
  2. {  
  3.     void main()  
  4.     {  
  5.         System.out.println("used this() our method invoked");  
  6.     }  
  7.     void sub()  
  8.     {  
  9.         this.main();  
  10.     }  
  11.     void sub1()  
  12.     {  
  13.         sub();  
  14.     }  
  15.     public static void main(String args[])  
  16.     {  
  17.         InvokeMethodEx im1 = new InvokeMethodEx();  
  18.         im1.sub1();  
  19.     }  
  20. }  
Output

fig-8.jpg

3.  Using the this() keyword to pass an argument in the constructor call.

It is useful if we use one object in many classes, as in:

  1. class Parent  
  2. {  
  3.     Children obj;  
  4.     Parent(Children obj)  
  5.     {  
  6.         this.obj = obj;  
  7.     }  
  8.     void print()  
  9.     {  
  10.         System.out.println(obj.data);  
  11.     }  
  12. }  
  13.   
  14. class Children  
  15. {  
  16.     int data = 10000;  
  17.     Children()  
  18.     {  
  19.         Parent b1 = new Parent(this);  
  20.         b1.print();  
  21.     }  
  22.     public static void main(String args[])  
  23.     {  
  24.         Children a = new Children();  
  25.     }  
  26. }  
Output

fig-10.jpg

4. Using the this() keyword to invoke the current class constructor.

For reusing constructors the this() keyword is a better approach.

  1. class Children  
  2. {  
  3.     int stid;  
  4.     String stname;  
  5.     Children()  
  6.     {  
  7.         System.out.println("default constructor is invoked");  
  8.     }  
  9.     Children(int stid, String stname)  
  10.     {  
  11.         this();  
  12.         this.stid = stid;  
  13.         this.stname = stname;  
  14.     }  
  15.     void print()  
  16.     {  
  17.         System.out.println(stid + " " + stname);  
  18.     }  
  19.     public static void main(String args[])  
  20.     {  
  21.         Children st1 = new Children(12"John");  
  22.         Children st2 = new Children(22"Sam");  
  23.         st1.print();  
  24.         st2.print();  
  25.     }  
  26. }  
Output

fig-7.jpg

5. Using the this() keyword to pass an argument in the method call.

We can pass an argument to our method. It is used in event handling, as in:

  1. class InvokeMethodEx  
  2. {  
  3.     void main(InvokeMethodEx obj)  
  4.     {  
  5.         System.out.println("used this() our method invoked");  
  6.     }  
  7.     void sub()  
  8.     {  
  9.         main(this);  
  10.     }  
  11.     public static void main(String args[])  
  12.     {  
  13.         InvokeMethodEx im1 = new InvokeMethodEx();  
  14.         im1.sub();  
  15.     }  
  16. }  
Output

fig-9.jpg

6.  Using the this() keyword to return the current class instance

We can return the this keyword as a statement from the methods.

Syntax for this:

  1. return_type_method_name()  
  2. {  
  3.     return this;  
  4. } 

Summary

 
In this article, we learned about super and this keywords of Java programming language and how to use them in Java program.