Java super and final keyword

java-Logo
Java Tutorials
Java Environment
Environment Setup
Declaration & Assignments
Flow Control
Miscellaneous
Java OOPs

super and final keywords are two popular and useful keywords in Java. They also play a significant role in dealing with Java programs and their classes. In this chapter, you will learn about how to use super and final within a Java program.

What is super in Java?

Super is a keyword of Java which refers to the immediate parent of a class and is used inside the subclass method definition for calling a method defined in the superclass. A superclass having methods as private cannot be called. Only the methods which are public and protected can be called by the keyword super. It is also used by class constructors to invoke constructors of its parent class.

Syntax:
super.<method-name>();

Usage of superclass

  • Super variables refer to the variable of a variable of the parent class.
  • Super() invokes the constructor of immediate parent class.
  • Super refers to the method of the parent class.

Here is an example of Java program which uses the super keyword

Example:
class employee {
 int wt = 8;
}

class clerk extends employee {
 int wt = 10;  //work time
 void display() {
  System.out.println(super.wt); //will print work time of clerk
 }

 public static void main(String args[]) {
  clerk c = new clerk();
  c.display();
 }
}
Output:
8

Instance refers an instance variable of the current class by default, but when you have to refer parent class instance variable, you have to use super keyword to distinguish between parent class (here employee) instance variable and current class (here, clerk) instance variable.

What is final in Java?

Final is a keyword in Java that is used to restrict the user and can be used in many respects. Final can be used with:

  • Class
  • Methods
  • Variables

Class declared as final

When a class is declared as final, it cannot be extended further. Here is an example what happens within a program

Example:
final class stud {
 // Methods cannot be extended to its sub class
}
class books extends stud {
 void show() {
  System.out.println("Book-Class method");
 }
 public static void main(String args[]) {
  books B1 = new books();
  B1.show();
 }
}

This will show an error that:

error: cannot inherit from final stud
class books extends stud{
^

This is because classes declared as final cannot be inherited.

Method declared as Final

A method declared as final cannot be overridden; this means even when a child class can call the final method of parent class without any issues, but the overriding will not be possible. Here is a sample program showing what is not valid within a Java program when a method is declared as final.

Example:
class stud {
 final void show() {
  System.out.println("Class - stud : method defined");
 }
}
class books extends stud {
 void show() {
  System.out.println("Class - books : method defined");
 }
 public static void main(String args[]) {
  books B2 = new books();
  B2.show();
 }
}

Variable declared as final

Once a variable is assigned with the keyword final, it always contains the same exact value. Again things may happen like this; if a final variable holds a reference to an object then the state of the object can be altered if programmers perform certain operations on those objects, but the variable will always refer to the same object. A final variable that is not initialized at the time of declaration is known as a blank final variable. If you are declaring a final variable in a constructor, then you must initialize the blank final variable within the constructor of the class. Otherwise, the program might show a compilation error.

Here is an example of a program in Java showing the use of the final variable

Example:
import java.util.*;
import java.lang.*;
import java.io.*;

class stud {
 final int val;
 stud() {
  val = 60;
 }
 void method() {
  System.out.println(val);
 }
 public static void main(String args[]) {
  stud S1 = new  stud();
  S1.method();
 }
}

What is blank or uninitialized final variable?

A final variable that is not initialized at the time of declaration is called the blank final variable. when you want to create a variable that is initialized at the time of creating an object and once initialized may not be changed, use the final keyword, and it will be beneficial in this case. For example PAN CARD number of an employee. It can be initialized only in the constructor.


Scroll Back to Top