Pages

Tuesday, 12 July 2022

Final variables Final methods and Final classes

 

Final variables, Final methods and Final classes
Final in Java is very important keyword and can be applied to class, method, and variables in Java.

Final variables
Any variable either a member variable or local variable (declared inside method or block) modified by final keyword is called final variable. Final variables are often declared with static keyword in java and treated as constant.
Ex:
public static final String LOAN = "loan";
LOAN = new String("loan") // error: cannot assign a value to final variable LOAN
Note: Final variables are by default read-only.
 
Example : Program on Final Variable.
class TestFinalVar
{
  public static void main(String[] args)
 {
  final int i=10;
  i=i+20;
  System.out.println(i);
 }
}
Output:
ubuntu@ubuntu:~/Desktop/rkjava$ javac TestFinalVar.java
TestFinalVar.java:6: error: cannot assign a value to final variable i
 i=i+20;
 ^
1 error

Final methods
A Java method with the final keyword is called a final method and it can not be overridden in the subclass. You should make a method final in Java if you think it’s complete and its behavior should remain constant in sub-classes.
In general, final methods are faster than non-final methods because they are not required to be resolved during run-time and they are bonded at compile time.
Example : Program on Final Method.
class Test
{
  void mnumber()
  {
  System.out.println("2345234523");
  }
  final void atmPin()
  {
  System.out.println("1234");
  }
}

class Abc extends Test
{
  void mnumber()
  {
  System.out.println("2345234123");
  }
  void atmPin()
  {
  System.out.println("1555");
  }
}
class TestFinalMet
{
 public static void main(String[] args)
 {
 Abc t =new Abc();
 t.mnumber();
 t.atmPin();
 }
}
 
Output:
ubuntu@ubuntu:~/Desktop/rkjava$ javac TestFinalMet.java
TestFinalMet.java:19: error: atmPin() in Abc cannot override atmPin() in Test
  void atmPin()
       ^
  overridden method is final
1 error
 
Ex:
class PersonalLoan
{
    public final String getName()
    {
    return "personal loan";
    }
}
class CheapPersonalLoan extends PersonalLoan
{
    //Override
    public final String getName()
    {
    return "cheap personal loan"; //compilation error: overridden method is final
    }
}

Output:
ubuntu@ubuntu:~/Desktop/rkjava$ javac PersonalLoan.java
PersonalLoan.java:11: error: getName() in CheapPersonalLoan cannot override getName() in PersonalLoan
    public final String getName()
                        ^
  overridden method is final
1 error

Final classes
The final class is complete in nature and can not be sub-classed or inherited. Several classes in Java are final e.g. String, Integer, and other wrapper classes.
 
Example : Program on Final Class.
final class Test
{
  void mnumber()
  {
  System.out.println("2345234523");
  }
  void atmPin()
  {
  System.out.println("1234");
  }
}

class Abc extends Test
{
  void mnumber()
  {
  System.out.println("2345234123");
  }
  void atmPin()
  {
  System.out.println("1555");
  }
}
class TestFinalMet
{
 public static void main(String[] args)
 {
 Abc t =new Abc();
 t.mnumber();
 t.atmPin();
 }
}
 
Output:
ubuntu@ubuntu:~/Desktop/rkjava$ javac TestFinalMet.java
TestFinalMet.java:13: error: cannot inherit from final Test
class Abc extends Test
                  ^
1 error
 
 
 
Ex:
final class PersonalLoan
{
}
class CheapPersonalLoan extends PersonalLoan
{
//compilation error: cannot inherit from final class
}
Output:
ubuntu@ubuntu:~/Desktop/rkjava$ javac PersonalLoan.java
PersonalLoan.java:5: error: cannot inherit from final PersonalLoan
class CheapPersonalLoan extends PersonalLoan
                                ^
1 error

No comments:

Post a Comment

Servlet - JSP Programs

JSP(Java Server Pages) Programs : JavaServer Pages (JSP) is a Java standard technology that enables you to write dynamic data-driven pages f...