this –
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="java"; void show(){ System.out.println(this.name); } }
(2) Refer current class constructor-
package dis; public class TisConst{ public TisConst(){ this(5); System.out.println("a"); } public TisConst(int age){ System.out.println("b"); } public static void main(String[] args){ TisConst k=new TisConst(); } }
(3) Invoke current class method-
package dis; public class TisMtd{ void show(String name){ System.out.println(name); } void disp(){ this.show("a"); } } }
(4)this : passed as an argument in method call
package dis; public class TisMtdArg{ String n="a"; void show(String name){ System.out.println(name); } void disp(){ this.show(this.n); } } }
(5)this : passed as an argument in constructor call
package dis; public class TisConstArg{ int a=12; void show1(){ new TisConstArg(this.a); } public TisConstArg(int age){ System.out.println("b"); } }
(6)this: can be used to return the class instance
package dis; public class TisClsIns{ TisClsIns currentIns(){ return this; } }
2]:- SUPER KEYWORD:-
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 SperVer { int age=20; void show(int age){ System.out.println(super.age); } } }
2:- Refer parent class constructor.
package spr; class SperConst{ SperConst(){ System.out.println("a"); } } public class SuprConst extends SperConst { SuprConst(){ super(); System.out.println("b"); } public static void main(String[] args){ SuprConst s1=new SuprConst(); } }
3:-Refer parent class method
package spr; class SperMtd{ void show(){ System.out.println("a"); } public class SuprMtd extends SperMtd{ void show(){ System.out.println("b"); } public static void main(String[] args){ SuprMtd s1=new SuprMtd(); s1.show(); } }