The Artima Developer Community
Sponsored Link

Java Buzz Forum
This and Super Keywords

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
This and Super Keywords Posted: Mar 8, 2015 6:20 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: This and Super Keywords
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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.       
  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

Read: This and Super Keywords

Topic: Inheritance Definition and Programs Previous Topic   Next Topic Topic: Program on encapsulation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use