Java super and final keywords

There are another two popular and effective using keywords of Java. These are the super and final keywords. They also play a major 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 sub class method definition for calling a method defined in super class. A super class 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 super class

  • Super variables refer to the variable of variable of parent class.
  • Super() invokes the constructor of immediate parent class.
  • Super refers to the method of 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 variable of current class is referred by instance 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 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 an compilation error.

Here is an example of a program in Java showing the use of 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 object and once initialized may not be changed, use final keyword and it will be very useful in this case. For example PAN CARD number of an employee. It can be initialized only in constructor.

No comments:

Post a Comment