Thursday 4 February 2016

What is this and super keywords in java

this and super keywords in java:
==========================

this:
------
this is a keyword used to refer current class variable.
We can use this keyword any where except inside the constructor

super:
--------
The super keyword in java is a reference variable that is used to refer immediate parent class object.
super is used to refer immediate parent class instance variable.
super is used to invoke immediate parent class method.

Sample example on this and super keywords
---------------------------------------------------------
package blog;

class Test1
{
int a=10;
String str="Ramesh selenium";

public void display() {

System.out.println("value of a is:"+this.a);
System.out.println("value of String is:"+this.str);
}

}

class Test2 extends Test1
{


public void sample() {

System.out.println("getting throgh super class"+super.str);
}
}

public class ThisSuperDemo {

public static void main(String[] args) {


Test2 test2 = new Test2();
test2.display();
test2.sample();
}


}

1 comment: