Thursday, 7 August 2014

What is final key word in java?

Final Key word in java:

final is the modifier applicable for classes, methods and variables.
If a method is not allowed to override in the child class then we should declare that method as ‘final’.
i.e for the final methods overriding is not possible.
 Ex:
  class P
{
   public final void marry()
   {
    System.out.println("Bujji");
   }
}
class C extends P
{
   public void marry()
   {
    System.out.println("Preeti");
   }
}
C.E: marry in ‘C’ can’t override, overridden method is final.
for any class if we are not allow to create child class, such type of classes we have to declare with
final i.e for final classes inheritance is not possible.
 Ex:
  final class P
{
}
class C extends P
{
}
C.E: can’t inherit from final ‘p’.
 
Relation Ship between final and abstract 
 
final method should not be overridden but we should override abstract method to provide
implementation. Hence final & abstract combination is always illegal.
 public abstract final void m1(){}
Illegal combination of modifiers : abstract, final.
For the final classes we are not allowed to create the child class. But for abstract class we
should create child class to provide implementation. Hence abstract and final are Illegal
combination of modifiers for that classes.
final classes can’t contain abstract methods but abstract classes can contain final methods.
Ex:

It is a good programming practice to use abstract keyword but If there is no specific
requirement it is not recommended to use ‘final’ keyword.

Final Instance Variables 
For the final instance variables JVM won’t provide any default values, compulsory we should
perform initialization before completion of constructor.

 The following are the places to perform this
1) At the time of declaration:.
final int i = 0;

2) Inside instance initialization class
 
   final int i;
   {
    i = 0;
   }
3) Inside constructor
  final int i;
   test()
   {
    i = 0;
   }
 If u r performing initialization any where else we will get compile time error.
   Ex:
    Class Test
    {
     final int i;
    }
O/P:- Generates Compile time error, variable “i” might not have been initialized.

   Class Test
   {
    final int i;
    public void m1()
    {
     i=20;
    }
   }
O/P:- Generates Compile time error, can’t assign a value to final variable.

   Class Test
   {
    final int i;
    {
     i=20;
    }
   }
O/P:- Won’t generate compile time error.

Final static Variables
 For the final static variables compulsory we should perform initialization.
   Ex:
class Test
{
     final static int i;
}
O/P:-generates Compile time error. variable  i might not have been assigned.
We can perform initialization for the final static variables in the following places.

1) At the time of declaration
final static int i = 0;
2) Inside static blocks
   static
   {
    i = 0;
   }
 
3) If u r performing initialization anywhere else we will get compile time error
   class Test
{
    final static int i;
    public static void main(String arg[])
    {
     i = 20;
    }
}
O/P:- will produce compile time error.


Final local Variables 
Before using a local variable(whether it is final or non-final) we should perform initialization. If we
are  not using local variable then no need of perform initialization even though it is final.
   Ex:
    class Test
{
     public static void main(String arg[])
     {
      int i;
      System.out.println("hello");
     }
}
This is valid Because we didn’t use “i” here.

class Test
{
    public static void main(String arg[])
    {
     final int i;
     System.out.println("hello");
    }
}
This is also valid because we didn’t use “i” here.

class Test
{
    public static void main(String arg[])
    {
     final int i = 0;
     System.out.println("hello");
    }
}
This is valid..

class Test
{
    public static void main(String arg[])
    {
     final int i;
     System.out.println(i);
    }
}
This is not valid. Because Before going to use we didn’t initialize “i”.

The variables which are declared as formal are acts as local variables of the method.
if a formal parameter declared as the final then with in the method we are not allowed to change it’s
values.
  Ex:
class Test
{
     public static void main(String arg[])
     {
      m1(10,20);
     }
     public static void m1(final int i,final int j)
     {
      //i=100;
      //j=100;
      System.out.println(i+"----"+j);
     }
}

If you use final you can’t  change i, j values.

Final parameter ‘i’ may not be assigned.

For the local variables the only applicable modifier is final.
   Ex:
class Test
{
    public static void main(String arg[])
    {
     private int i = 20;
     System.out.println(i);
    }
}
O/P:- Provides compile time error because we declare int as private.



No comments:

Post a Comment