- 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.
Top 10 Java interview questions and programs on this keyword
- package com.instanceofjava;
- public class ThisDemo {
- int a, b;
- ThisDemo(int a, int b){
- a=a;
- b=b;
- }
- public static void main(String[] args){
- ThisDemo obj= new ThisDemo(1,2);
- System.out.println(obj.a);
- System.out.println(obj.b);
- }
- }
Output:
- 0
- 0
- Without this keyword if variable names are same we can not assign instance variables so this problem will solve by using this keyword
- package com.instanceofjava;
- public class ThisDemo {
- int a, b;
- ThisDemo(int a, int b){
- this.a=a;
- this.b=b;
- }
- public static void main(String[] args){
- ThisDemo obj= new ThisDemo(1,2);
- System.out.println(obj.a);
- System.out.println(obj.b);
- }
- }
Output:
- 1
- 2
Used to invoke current class constructor:
- package com.instanceofjava;
- public class ThisDemo {
- int a, b;
- ThisDemo(){
- System.out.println("Default constructor called");
- }
- ThisDemo(int a, int b){
- this();
- this.a=a;
- this.b=b;
- }
- public static void main(String[] args){
- ThisDemo obj= new ThisDemo(1,2);
- System.out.println(obj.a);
- System.out.println(obj.b);
- }
- }
Output:
- Default constructor called
- 1
- 2
Used to call current class method:
- package com.instanceofjava;
- public class Sample{
- int a, b;
- Sample(int a, int b){
- this.a=a;
- this.b=b;
- }
- void show(){
- System.out.println("Show() method called");
- }
- void print(){
- this.show();
- System.out.println(obj.a);
- System.out.println(obj.b);
- }
- public static void main(String[] args){
- Sample obj= new Sample(1,2);
- obj.print()
- }
- }
Output:
- Show() method called
- 1
- 2
super keyword:
- Predefined instance variable used to hold super class object reference through sub class object.
Super keyword interview questions java
Used to call super class constructor:
- package com.instanceofjava;
- public class A{
- int a, b;
- A(int a, int b){
- a=a;
- b=b;
- System.out.println("super class constructor called");
- }
- }
- package com.instanceofjava;
- public class B extends A{
- int a, b;
- A(int a, int b){
- super(a,b);
- a=a;
- b=b;
- }
- public static void main(String[] args){
- B obj= new B(1,2);
- }
- }
- }
Output:
- super class constructor called
- 1
- 2
Used to call super class methods from subclass:
- package com.instanceofjava;
- public class A{
- int a, b;
- A(int a, int b){
- a=a;
- b=b;
- System.out.println("super class constructor called");
- }
- void add(){
- System.out.println("super class method called");
- }
- }
- package com.instanceofjava;
- public class B extends A{
- int a, b;
- A(int a, int b){
- super(a,b);
- a=a;
- b=b;
- }
- void print(){
- super.add();
- System.out.println(this.a);
- System.out.println(this.b);
- }
- public static void main(String[] args){
- B obj= new B(1,2);
- obj.print();
- }
- }
- }
Output:
- super class method called
- 1
- 2
No comments