Way2Java

final Keyword Java Examples Screenshots

final Keyword Java

After going through "static" and "super" keywords, let us go another keyword "final". These three keywords, known as access modifiers, are used very often in Java code. The final keyword behaves very differently when applied to variables, methods and classes. Even though its functionality (purpose) is different in the three contexts, but one thing is common; it does not allow the programmer to do certain actions.

  1. A final variable cannot be reassigned.
  2. A final method of the super class cannot be overridden by subclass
  3. A final class cannot be extended by any other class. Final class does not fit for inheritance.

Three programs are given to explain the above three rules.

1. final Keyword with Variables

"const" keyword of C/C++ is not supported by Java and in its place, Java uses "final" keyword. That is, a final variable cannot be reassigned. Once assigned, cannot be modified later.

public class Book
{
  public static void main(String args[])
  {
    int cost = 400;                          // non-final, can be modified later
    System.out.println("Original Book cost Rs." + cost);
    cost = 500;
    System.out.println("Modified Book cost Rs." + cost);

    final int packing = 50;                  // final, cannot be modified later
    System.out.println("Packing cost Rs." + packing);
    // packing = 100;                        // raises error
  }
}
Output screen of Book.java

In the above code, the variable cost is declared as non-final and packing as final. The cost variable is reassigned to 500 but packing cannot be(if the comments are removed in the code, it is compilation error). After comfortable with final variables let us go to final methods.

2. final Keyword with Methods

In inheritance, the subclass is at liberty to use super class methods or override and use its own methods. Moreover, with "super" keyword, it can make use of its own and that of super class also. This is possible as long as super class permits. We know earlier subclass is rich; but now I say one more "super class is powerful". Subclass can play with super class as long super class permits. At any time, super class can place some restrictions on subclass about it' members usage.

Now the super class would like to place a small restriction on subclass; it says, few methods of it's cannot be overridden. The super class declares those methods as final. That is, final methods of the super class cannot be overridden.

class Bird
{
  public final void eat()             // final method, cannot be overridden
  {
    System.out.println("Birds eat");
  }
  public void fly()	              // non-final method, can be overridden
  {
    System.out.println("Birds fly");
  }
}
public class Peacock extends Bird
{
  public void fly()	
  {
    System.out.println("Peacock flies small distances");
  }
  public static void main(String args[])
  {
    Peacock p1 = new Peacock();
    p1.eat();                         // calling super class final method
    p1.fly();                         // calling its own overridden method
  }
}
Output screen of Peacock.java

In Bird's class, eat() method is declared as final and fly() as non-final. The eat() method cannot be overridden by the subclass Peacock and regarding fly() method, subclass is at liberty to override (method overriding). In the above code, it is overridden. Infact, the subclass can call super class fly() method also with super.fly() (not used in the above code).

3. final Keyword with Classes

In inheritance, a class can be extended by another class; but it is at the mercy of the super class. If the super class does not like any one to inherit, simply it declares its class as "final". A final class cannot be extended by any other class. A final class does not fit for inheritance.

final class Spacecraft       	// class is final
{
  public void fly()
  {
    System.out.println("Spacecraft flies");
  }
}
public class Aeroplane
{
  public void fuel()
  {
    System.out.println("Aeroplane uses Hi-Octane petrol");
  }
  public static void main(String args[])
  {
    Aeroplane a1 = new Aeroplane();
    a1.fuel();
            
    Spacecraft s1 = new Spacecraft();
    s1.fly();                  // calling final class method with composition
  }
}
Output screen of Aeroplane.java

Spacecraft class is declared as final and for this reason, Aeroplane cannot extend it. But Aeroplane class can make use of final class methods by "composition".