Thursday, 21 May 2020

Key difference between final, finally, finalize in java


Key difference between final, finally, finalize in java
final in java:
final is a keyword applicable for variables methods and classes.
final variables:
                Variables declared with final keyword are called as final variables. In java if a variable is declared as final then its value can’t be changed during program execution.
                when you don’t want to change the value of variable then declare them as final.
                final variables has to be initialized before use.
                                syntax:
                                                final <datatype> <var_name> = value;
                                                final int x=100;
final methods:
                In java if any method is declared with final keyword then they are called as final method. when ever we declare any method as final then we can’t override these methods in the sub classes.
                               
                                class Demo{
                                     void m1{
                                                    System. out. println( "m1 method of demo class");
                                                 }
                                                final void m2{
                                                    System. out. println( "m2 method of demo class");
                                                    System. out. println( "final method of demo class");

                                                 }
                                }
               
                  In above example we declared m2 as final hence we can’t override  this method in the sub classes of Demo class.
final classes
                classes declared with final keyword are called as final classes. These class can’t be inherited.
                A final class can’t be declared as abstract class.
               
                  final class  Demosuper{
                                                void m1{
                                                    System. out. println( "m1 method of demo super class");
                                                 }
                                                void m2{
                                                    System. out. println( "m2 method of demo super class");
                                                 }
                  }
                 
                The above class is declared as final class thence we can’t create the sub class for Demosuper class. Hence we cant cant inherit these classes. 
                 
finally:
                                finally is a keyword in java that is applicable for blocks.
                                whenever we want anything to execute independent of whether exception is raised or not then we go for finally.
                                               
                                                finally{
                                                   ......
                                                   ...... // some statements.
                                                   ......
                                                }
                                               
finalize:
                This is a method in java programming language. This method belongs Object Class which is the super class of all classes in java.
                Before the object is garbage collected by JVM finalize method is called.

Answer these questions….
Can we extend final class in java?
What is the basic use of finalize method in java.
Who calls finalize method in java programming language.            
When finalize method is called.
Is finalize guaranteed to be called.
What is the main purpose of final keyword in java?
What is final finally finally in java?
What is the main use of finally in java?
               

No comments:

Post a Comment