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.
- A final variable cannot be reassigned.
- A final method of the super class cannot be overridden by subclass
- 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
}
}

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
}
}

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
}
}

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".
sir,
somewhere i read ..
Parent p = new Child();( refferential polymorphism )
i had put above statement n came to know that results were no different than
Child p = new Child(); ( compile time polymorphism )
as in both cases it will show non overridden methods from parent and overridden methods from child , is there any difference or benefit of using run time polymorphism over compile time polymorphism ?
Both has their own advantages and applications.
sir, but i m not able to think of any scenario where these two ways hold different meaning or what advantege other has over first one , can you please explain me with an example or anything .
Thanks .
final works in three ways. Three has different purposes.
final work in three way-
1-final variable initialized at declared time.
2-initial by block(init block or static block at once in program)
3-by the help of constructor
sir i am little confusion final variable can’t have default value by JVM that called blank final.
but in my program why JVM initialized final variable by default value.
class test
{
static final int x=m();
static int m()
{
System.out.println(“x= “+x);
return 100;
}
public static void main(String args[])
{
System.out.println(“x= “+x);
}
}
output:-
x=0
x=100
You are giving value through a method call or as well you can give through array or try with keyboard.
sir,
what is the need of overriding.??? instead of override why can’t we declare directly in superclass itself what we wants.
could u please explain…???
If the subclass wants the same method of super class but with different functionality (output), then subclass overrides; else not necessary.