» » This and Super Keywords


  • this keyword used to store current object reference.
  • super keyword used to store super class reference in subclass object.

this keyword:

  • Predefined instance variable to hold current object reference.

It must used explicitly if non-static variable and local variable name is same.

 

 





  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.         a=a;
  9.         b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 0
  2. 0
  • Without this keyword if variable names are same we can not assign instance variables so this problem will solve by using this keyword

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.         this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 1
  2. 2

Used to invoke current class constructor:

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6. ThisDemo(){
  7. System.out.println("Default constructor called");
  8. }

  9.  ThisDemo(int a, int b){
  10.        this();
  11.         this.a=a;
  12.        this.b=b;
  13.  }
  14.  
  15.     public static void main(String[] args){
  16.  
  17.         ThisDemo obj= new ThisDemo(1,2);
  18.         
  19.         System.out.println(obj.a);
  20.         System.out.println(obj.b);
  21. }
  22. }

Output:

  1. Default constructor called
  2. 1
  3. 2

Used to call current class method:

 

  1. package com.instanceofjava;
  2.  
  3. public class Sample{
  4.     int a, b;
  5.  

  6.  Sample(int a, int b){
  7.       
  8.        this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12. void show(){
  13.  
  14. System.out.println("Show() method called");
  15.    
  16. }
  17.  
  18. void print(){
  19.  
  20.     this.show();
  21.     System.out.println(obj.a);
  22.     System.out.println(obj.b);
  23.  
  24.  }
  25.     public static void main(String[] args){
  26.  
  27.         Sample obj= new Sample(1,2);
  28.         
  29.       
  30.         obj.print()
  31. }
  32. }



Output:

  1. Show() method called
  2. 1
  3. 2

super keyword:

  • Predefined instance variable used to hold super class object reference through sub class object.

 

Used to call super class constructor:



  1. package com.instanceofjava;
  2.  
  3. public class A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.  
  8.         a=a;
  9.         b=b;
  10. System.out.println("super class constructor called");
  11.  
  12.  }
  13.  
  14. }


  1. package com.instanceofjava;
  2.  
  3. public class B extends A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.     super(a,b);
  8.         a=a;
  9.         b=b;
  10. }
  11. public static void main(String[] args){
  12.  
  13.         B obj= new B(1,2);
  14.         
  15.     
  16. }
  17.  
  18.  }
  19.  
  20. }

Output:

  1. super class constructor called
  2. 1
  3. 2

Used to call super class methods from subclass:


  1. package com.instanceofjava;
  2.  
  3. public class A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.  
  8.         a=a;
  9.         b=b;
  10. System.out.println("super class constructor called");
  11.  
  12.  }
  13.  
  14. void add(){
  15. System.out.println("super class method called");
  16. }
  17.  
  18. }


  1. package com.instanceofjava;
  2.  
  3. public class B extends A{
  4.     int a, b;
  5.  
  6.  A(int a, int b){
  7.     super(a,b);
  8.         a=a;
  9.         b=b;
  10. }
  11. void print(){ 
  12. super.add();
  13. System.out.println(this.a);
  14. System.out.println(this.b);
  15.  


  16. }
  17. public static void main(String[] args){
  18.  
  19.         B obj= new B(1,2);
  20.         
  21.        obj.print();
  22. }
  23.  
  24.  }
  25.  
  26. }

Output:

  1. super class method called
  2. 1
  3. 2

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu