Saturday, 17 December 2016

USE OF FINAL KEYWORD IN JAVA

Tags

      Its 3 uses are :                                          


1.To prevent method overriding

2.To prevent overloading

3. To declare final variable
______________________________________________________

Examples :

1.       public class base {
                final void get(){
                                    System.out.println("get called");
                                        }

                             }


public class der extends base{

void get(){                          //error , cannot override final method from base//
}
public static void main(String[] args) {
}

}

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

2.              final public class base {
                                      }

public class der extends base{            // the type final base class can't have der as subclass//

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

}

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

3.    *  to declare a final variable in main use this  :    final int sum =100

       * note : value declaration is necessary for final constant variable
      
        * to declare a final variable out of class (can be accessed without creating object) and visible to              every class in package use this :  
                                                                public static final int sum = 200;







Enter Your Comments Below Emoticon