Super vs Final vs This Keyword In Java

In this chapter, we will learn about Super vs Final vs This Keyword In Java. These 3 keywords are very useful while writing Java classes and programs.

1. What is Super in Java?

Remember Inheritance? Super keyword in Java plays a major role in Inheritance because we can use the super keyword to call parent class methods.

In other words, we can use the super keyword to call methods and data members of the immediate parent class.

  • Whenever we create an instance of the child class, then the instance of the parent class is created implicitly, which is referred by the super reference variable.
  • If we have the same method name in a child as well as parent class then the super keyword is used to call the parent class method.

Let’s take an example where both classes have the same data member:

Parent Class:

public class ParentClass {
	int firstNumber = 10;
}

Child Class:

public class ChildClass extends ParentClass {
	int firstNumber = 3;

	void printNumber() {
		System.out.println("Value = " + firstNumber);
	}

	public static void main(String[] args) {
		ChildClass cal = new ChildClass();
		cal.printNumber();
	}
}

Output:

Value = 3  // child class member

Now, let’s take an example using the super keyword:

Parent Class:

public class ParentClass {
	int firstNumber = 10;
}

Child Class:

public class ChildClass extends ParentClass {
	int firstNumber = 3;

	void printNumber() {
		System.out.println("Value = " + super.firstNumber);
	}

	public static void main(String[] args) {
		ChildClass cal = new ChildClass();
		cal.printNumber();
	}
}

Output:

Value = 10 // printing parent class value

2. What is Final in Java?

Final is a keyword in Java and we can use the Final keyword to restrict the use of variable, method, or class. Therefore, value once assigned to any variable can never be changed. We can use the Final keyword with:

  • Variable
  • Method
  • Class

2.1 Final Variable:

As I mentioned, we can’t change the value to a final variable and its value will remain the same throughout the program. Otherwise, JVM will through an error.

public class ParentClass {

	final int number = 10;
	
	public void changeValue() {
		number = 20; // compilation error
	}
	public static void main(String[] args) {
		ParentClass obj = new ParentClass();
		System.out.println(obj.number);
	}
}

Output:

Compilation error saying: The final field ParentClass.number cannot be assigned.

2.2 Final Method:

We can’t override a final method. Let’s take an example:

public class ParentClass {
	final void printing() {
		System.out.println("From Parent Class");
	}
}
public class ChildClass extends ParentClass {
	@Override
	public void printing() { // compile error
		System.out.println("From Child Class");
	}

	public static void main(String[] args) {
		ChildClass cal = new ChildClass();
		cal.printing();
	}
}

Output:

If you try to run ChildClass then it will throw a compilation error saying Cannot override the final method from the parent class.

2.3 Final Class:

We can’t extend/inherit a final class. Let’s take an example:

final class ParentClass {
	
}
public class ChildClass extends ParentClass {

}

Output:

Compilation error saying: The type ChildClass cannot subclass the final class ParentClass.

3. What is This in Java?

As the name defines, this refers to the current object and it is a reference variable.

public class TestClass {

	int firstNumber;
	int secondNumber;

	TestClass(int a, int b) {
		this.firstNumber = a;
		this.secondNumber = b;
	}

	void display() {
		System.out.println("FirstNumber value = " + firstNumber);
		System.out.println("SecondNumber value = " + secondNumber);
	}

	public static void main(String[] args) {
		TestClass obj = new TestClass(10, 20);
		obj.display();
	}
}

Points to Remember:

  • “THIS” keyword is a reference variable.
  • It can be used to refer to the current class object.
  • We can use this keyword as an argument.
  • this can be used in the return statements.

Official Docs: Visit Here.

I hope you learned about This, Super and Final Keyword In Java from this chapter or refreshed your existing knowledge base.