Step 17:this and super keywords – 11th hour + code

this –

this-keyword-uses-in-java-featured-image-1280x720[1]

Definition-

It’s a keyword which refers to current class.
this keyword is used to-
(1) Refer class variable –

 package dis;

public class TisVar{

String name="Kuldeep";

void show(){
System.out.println(this.name); //here
}

public static void main(String[] args){
TisVar tvObj = new TisVar();
tvObj.show();
}

}

Run this Program ▶️

(2) Refer current class constructor-

 package dis; public class TisConst{

public TisConst(){

this(5);

System.out.println("Kuldeep");

}

public TisConst(int age){

System.out.println("kaushik");

}

public static void main(String[] args){
TisConst k=new TisConst();
}

}

Run this Program ▶️

(3) Invoke current class method-

 package dis;

public class TisMtd{

void show(String name){

System.out.println(name);

}

void disp(){

this.show("kuldeep");

}

public static void main(String[] args){
TisMtd tisObj = new TisMtd();
tisObj.disp();
}

}

Run this Program ▶️

(4)this : passed as an argument in method call

 package dis;

public class TisMtdArg{

String name = "kuldeep";

void show(String name){

System.out.println(name);

}

void disp(){

this.show(this.name); //here

}

public static void main(String[] args){
TisMtdArg tisObj = new TisMtdArg();
tisObj.disp();
}

}

Run this Program ▶️

(5)this : passed as an argument in constructor call

 package dis;

public class TisConstArg{

int age = 12;

void show1(){

new TisConstArg(this.age);

}

public TisConstArg(int age){

System.out.println("Kuldeep - Age = "+age);

}

public static void main(String[] args){
TisConstArg tisConstArgObj = new TisConstArg(30);
}

}

Run this Program ▶️

(6)this: can be used to return the class instance

 package dis;

public class TisClsIns{

TisClsIns currentIns(){

return this; //here

}

public static void main(String[] args){
TisClsIns tisClsInsObj = new TisClsIns();
TisClsIns tisClsInsObj2 = tisClsInsObj.currentIns();
System.out.println(tisClsInsObj2);
}

}

Run this Program ▶️

2]:- SUPER KEYWORD:-

super-keyword-in-java-featured-image-1280x720[1]

Definition-

It’s a keyword which refers to parent class.

super keyword is used to –
1:- Refer parent class instance variable.

 package spr; class SperVar{
int age =10;
}
public class SuprVar extends SperVar {
int age=20;
void show(){
System.out.println(super.age);
}
public static void main(String[] args){
SuprVar suprVarObj = new SuprVar();
suprVarObj.show();
}
}

Run this Program ▶️

2:- Refer parent class constructor.

 package spr; class SperConst{
SperConst(){
System.out.println("parent");
}
}
public class SuprConst extends SperConst {
SuprConst(){
super();
System.out.println("child");
}
public static void main(String[] args){
SuprConst s1=new SuprConst();
}
}

Run this Program ▶️

3:-Refer parent class method


package spr;
<div class="line number2 index1 alt1">

class SperMtd{
void show(){
System.out.println("parent");
}
}

public class SuprMtd extends SperMtd{
void show(){
super.show();
System.out.println("child");
}
public static void main(String[] args){
SuprMtd s1=new SuprMtd();
s1.show();
}
}

</div>
<div class="line number15 index14 alt2"><code class="java plain"></code>