Tuesday, 3 June 2014

Difference between final,finalize and finally in Java

1) final keyword:-

          The final keyword is used to restrict the user. This variable can be applied to variable,method and class.
          
        If you declare the variable as final, you can not change the value of the final variable.(i.e reassignment is not possible for final variables)
     
Example ,

final String name="anil";
name="mahesh";   //it will give compile time error.

        
    If you declare the method as final then you can not override the method in the sub class. Overridden is not possible for the final methods.

Example:-

class Employee{

     public final String details(){                                                 
           return "details";
     }
}

class Manager extends Employee{

    public String details(){                  //will give error                                       
          return "details manager";
    }
}                      


If you declare class as final then you can not extend that class means inheritance is not possible for final classes.
  
Example,

final  class Employee{
        //some statements
}

class Manager extends Employee{          //compilation error,cannot inherit
}


2) finally:

                  finally block is optional and can be used only with try-catch block. Since exception halts the process of execution, we might have some resources open that will not get closed, so we can use finally block. finally block gets executed always, whether exception occurred or not.(Except calling System.exit(1)).

Example,

try{
                                                     
    //some statements

} catch(Exception e){ 
                                                   
    //exception handling block

} finally{

    //e.g close database connection interface,
   //e.g close file resources & so on.....

}
  

3) Finalize() method:-

       Before an object is garbage collected, the runtime system calls its finalize() method. The intent is for finalize() to release system resources such as open files or open sockets before getting collected.

      Your class can provide for its finalization simply by defining and implementing a method in your class named finalize(). Your finalize() method must be declared as follows:

        protected void finalize () throws throwable

    This class opens a file when its constructed:

 class OpenFile {
       FileInputStream aFile = null;
       OpenFile(String filename) {
             try {
                  aFile = new FileInputStream(filename);
             } catch (java.io.FileNotFoundException e) {
                  System.err.println("Could not open file " + filename);
             }
       }
 }

    To be well-behaved, the OpenFile class should close the file when its finalized. Here's the finalize() method for the OpenFile class:

protected void finalize () throws throwable {
      if (aFile != null) {
             aFile.close();
             aFile = null;
      }
}

      The finalize() method is declared in the java.lang.Object class. Thus when you write a finalize() method for your class you are overriding the one in your super class.

Why not use Finalize() method :-

      There are some reasons not to use finalize() method are as follows:
  • finalize() execution is not guaranteed at all.
  •  finalize() methods do not work in chaining like constructors. It means like when you call a constructor then constructors of all super classes will be invokes implicitly. But, in case of finalize methods, this is not followed. Super class’s finalize() should be called explicitly.
  •  Any Exception thrown by finalize method is ignored by G.C thread and it will not be propagated further, in fact it will not be logged in your log files.
  •  finalize() add heavy penalty in performance.



 Related Post:--
1) Exception Handling Interview questions and answers
2) OOP's Concepts in Java and explanation
3) String Interview Questions and Answers
4) How many types of Polymorphism in Java? Explain in brief.
5) Difference between Loose Coupling and Tight Coupling in Java With Examples.

No comments:

Post a Comment