Wednesday, March 8, 2017

Final Keyword

Tags

Final Keyword is used to make any value immutable(not changable),avoid inheritance,or avoid method overriding.

Final To Avoid Method Overriding:

Code::
class Sample
{
  final void show()
    {
        System.out.println("Hello NT");
    }
}
class Test extends Sample
{
    /*void show()
    {
        Sytsem.out.println("Hello NTI");
    }*/          //Compile Time Error
}
public class FinalKeyword 
{

    public static void main(String[] nt) 
    {
      Test t=new Test();
      t.show();
    }
    

}
Output::


Final To avoid Inheritance :

Code::
final class Sample
{
     void show()
    {
        System.out.println("Hello NT");
    }
}
/*class Test extends Sample
{
  
}*/ //compile time error
public class FinalKeyword 
{

    public static void main(String[] nt) 
    {
      
    }
    
}