This site javatpoint is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.

Tuesday, 31 October 2017

FINAL KEY WORD IN JAVA

FINAL KEY WORD IN JAVA

FINAL keyword in java is used mainly three concepts

1.variables

2.methods

3.class


1.variables. if we use final keyword with variables then we assinged the value of variable once than we can not change another.


syntax:  final int a;

example:


class sum
{
               public static void main(String args[])
               {
                final int a=10;

               void xyz()
              {
                int a=20; // change value variable
                }
           }
}


out put  compile time error


2.method: if we use final keyword with method then it can not be over ride in sub class.



syntax: final void m1()
                    {
                   //  statements;
                     }

example: 

class Sample
{
               public static void main(String args[])
               {

               int a=10;

               final xyz()
              { 
               System.out.println("hello java learning point");
               }

              }

}


class Test extends Sample
{

                  void xy()
                   {
                    System.out.println("good morning");
                    }

                   Test ob=new Test();
                   ob.xyz();
}




output 

compile time error


3. class : if we use final keyword with class then these class can not be extends in another class.

syntax:      final class
                 {
                 //body of class

                   }

example:



class Sample
{
               public static void main(String args[])
               {

               int a=10;

               final xyz()
              { 
               System.out.println("hello java learning point");
               }

              }

}


class Test extends Sample
{

                  void xy()
                   {
                    System.out.println("good morning");
                    }

                   Test ob=new Test();
                   ob.xyz();
}


output compile time error







Adbox