Final Class:
If a class is declared as final, it cannot be inherited by derived classes.
final variable:
If a variable is declared as final, it can initialized only once. Its not mandatory to initialize the final variable at the time of declaration. If its not initialized at the time of declaration, it called blank final variable.
Example of final variable
There is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
}//end of class
**Output** :Compile Time Error
Final Method:
If a method is declared as final, it cannot be overridden by derived classes.
Example of final method
class Bike{
final void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
Output:Compile Time Error