Thursday, March 30, 2017

Final modifier in java


Final modifier :- 

Final is the modifier applicable for classes method and variables

Final method :- 

whatever methods present has by default available to the child through inheritance. If the child not satisfied with parent method implantation then child is allowed to redefine that method based on it’s requirement. This process is called overriding.

รจ  If the parent method is declared as final then we can’t override that method in the child class because its implementation is final.

Ex :-  package com.javahighq.test;


public class Test {
      public void property(){
            System.out.println("cash & gold");
      }
      public final void marry(){
            System.out.println("marry raja");
      }
}
class p extends Test{
      public void marry(){
            System.out.println("gopal marry");
//Cannot override marry method because  marry method is final method from //Test
      }
}

Final class :-

If a class declared as final we can’t extend functionality of class i.e we can’t create child class for that class. i.e inheritance is possible for final class

Ex :-

final class p {
     
}
class c extends p{
      public static void main(String[] args) {
            System.out.println("chield class");
            //The type c cannot subclass the final class p
      }
}

Note:-

Every method present indise final class is always final by default but every variable present inside final class need not be final.

Ex: - final class p{
      static int  x=10;
      public static void main(String[] args) {
            x=77;
            System.out.println(x);//77
      }
}

The main advantage of final keyword is we can achieve security and we can provide unique implementation.
But the main disadvantage is final keyword is we are missing key benefits as oops: inheritance (because of final classes) and polymorphism (because of final methods) hence if there is no specific requirement then it is not recommended to use final keyword.

Final variable:-

If the value of a variable is varied from object to object   such type of variable are called inductance variables
For every object a separate copy of instance variables will be created.
For instance variables we are not required to perform initialization explicitly JVM will always provide default values.


Ex:-

class test{
      int x;
      public static void main(String[] args) {
            test teset=new test();
            System.out.println(teset.x);//0
      }
}

If the instance variable declared as final then compulsory we have to perform initialization explicitly weather we are using or not and JVM won’t provide default values.


Ex :-

class test{
final int x;
//The blank final field x may not have been initialized
     
}


Rele:- 

For final instance variable compulsory we should perform intatilzation before constructor completion.
The following are various places for initialization.
At the time of declatation
Onside instance block
Inside constructor
At the time of declaration


class test{
final int x=10;
     
}
Inside instance block
class test{
final int x;
{
x=10;
}

In side constructor

class test{
final int x;
      test(){
      x=10;
}
}

These are the only possible places to perform initialization for final instance variables.
If we are trying to perform initialization any where else then we wll get compiletime error.

Ex :-

class test{
final int x;
      public void m1(){
x=10;
}
//cannot assign a value to final variable x
}

No comments:

Post a Comment