this and super in java

Q. What is this and super in java? What are uses of this and super?

this keyword in java – 

  1. this is a keyword which refers current class object.
  2. this is used to invoke current class data members(variable,method and constructor).
  3. this can’t be used in static context i.e static method or static block. It will give compilation error.
  4. this can be used as a parameter.
  5. we can’t assign any value to this.

Example 1 -this is a keyword which refers a current class object.

public class ThisExample1 {

	public void m1() {

		System.out.println("this is value of this keyword  " + this);
	}

	public static void main(final String[] args) {

		ThisExample1 thisExample1 = new ThisExample1();

		thisExample1.m1();

		System.out.println("this is value of object        " + thisExample1);
	}
}

Output of above program –

 

Example 2 – this is used to invoke current class data members(variable,method and constructor).

public class ThisExample2 {

	int id = 10;

	public String m1() {
		return "called from this";
	}

	ThisExample2() {

		System.out.println("this is default constructor ");
	}

	ThisExample2(String name) {
		this(); // used to call current class constructor
		System.out.println("instance variable  " + this.id);
		System.out.println("instance method    " + this.m1());
		
		
	}


	public static void main(String[] args) {

		ThisExample2 thisExample2 = new ThisExample2("");

	}
}

Output is –

this is a default constructor
instance variable 10
instance method called from this

Note –  even if we do not use this with an instance variable id and method m1() it will work fine.

 

Example 3 –  this can’t be used inside a static context.



 

Example 4 – this can be use as parameter.

public class ThisExample2 {

	public void m1(ThisExample2 this) {
		System.out.println(this);
	}

	public static void main(String[] args) {
		ThisExample2 example2 = new ThisExample2();
		example2.m1();
	}
}

 

Some more example –

 

public class ThisExample2 {

	String name;

	ThisExample2(String name) {
		this.name = name;
	}

	public static void main(String[] args) {

		ThisExample2 example2 = new ThisExample2("ram");

	}
}

In above program we have name as instance variable and also as local variable.

this.name at line number 6 refers to instance variable where as name refers to local variable.The value which we will pass in  object i.e ram, will assign to instance variable name. This is common practice to access instance variable with this keyword.

 

Super keyword in java – 

  1. The super is a keyword which refer immediate super class object.
  2. super is used to invoke parent class data members(variable,method and constructor).
  3. super can’t be used in static context.

Example – super is used to invoke parent class data members(variable,method and constructor).

class SubExample {
	int a = 10;

	SubExample() {
		System.out.println("super class constructor");
	}

	public String m1() {
		return "this is a super class method";
	}
}

public class SuperExample1 extends SubExample {

	SuperExample1() {
		super();// accessing super class constructor.
		System.out.println(super.a);// accessing super class instance variable.
		System.out.println(super.m1());// accessing super class method.
	}

	public static void main(String[] args) {
		SuperExample1 example1 = new SuperExample1();
	}
}

 

Output is –

super class constructor
10
this is a super class method

 

 

 

Basic Core Java Interview Question