Along with various other keywords Java also provides this and super as special keywords which primarily used to represent current instance of a class and it's super class respectively. With this similarity both these keywords have significant differences between them which are listed as below -
Sr. No. | Key | this | super |
---|---|---|---|
1 | Represent and Reference | this keyword mainly represents the current instance of a class. | On other hand super keyword represents the current instance of a parent class. |
2 | Interaction with class constructor | this keyword used to call default constructor of the same class. | super keyword used to call default constructor of the parent class. |
3 | Method accessibility | this keyword used to access methods of the current class as it has reference of current class. | One can access the method of parent class with the help of super keyword. |
4 | Static context | this keyword can be referred from static context i.e can be invoked from static instance. For instance we can write System.out.println(this.x) which will print value of x without any compilation or runtime error. | On other hand super keyword can't be referred from static context i.e can't be invoked from static instance. For instance we cannot write System.out.println(super.x) this will leads to compile time error. |
Example of this vs. super
Equals.jsp
class A { public int x, y; public A(int x, int y) { this.x = x; this.y = y; } } class B extends A { public int x, y; public B() { this(0, 0); } public B(int x, int y) { super(x + 1, y + 1);// calls parent class constructor this.x = x; this.y = y; } public void print() { System.out.println("Base class : {" + x + ", " + y + "}"); System.out.println("Super class : {" + super.x + ", " + super.y + "}"); } } class Point { public static void main(String[] args) { B obj = new B(); obj.print(); obj = new B(1, 2); obj.print(); } }
Output
Base class : {0, 0} Super class : {1, 1} Base class : {1, 2} Super class : {2, 3}