STUDENTS 3K

  • Home
  • Students+
    • Engineering Students
    • Education Loan
    • Study Abroad Guide
    • Projects Download
  • Freshers
    • Aptitude Q & A
    • Placement Papers
    • Verbal Ability
    • Interview Questions
    • IT Company Details
    • Job Updates
  • Study Resources
    • Career Guidance
    • LAB Programs
      • C Programs
      • CPP Programs [C++]
      • Java Programs
    • Question Papers
    • Learn English
    • Notice Board
  • More –>>
    • Love calculator
You are here: Home / Interview / What is Final Modifier?

What is Final Modifier?

Final modifier in Java is used to indicate a method cannot be overridden in a derived class. When final modifier is declared it prevents the class from acting as a base class and stops the derived class. This process will prevent late binding which in turn stops polymorphism. Final modifiers will always precede the statement as shown in the example.

private Booln EmpDetails;
Static final Wk= 9;
protected static final int Sal= 5000;
public static void main(String[ ] arguments)
{
// This method’s body content
}

The statement bodies which are in bold are final modifiers in the above program. Unlike class, final modifier applied to instance will simply mean that the instance variable cannot be changed.

  • Many: Java interview Q&A available here

How to use Final Modifier in Java?

Now it’s clear for you that if you create a final int variable and initialize a value, say 5000 now you cannot the change the value for this variable.

You can make a class as final and protect it from inheritance but you can’t extend the final class in Java or modify the methods of final class. Also no inner or anonymous classes can be created. There are three kinds of variables used in Java they are:

  • Static final variable
  • Non-static final variable
  • Local final variable

Static and Non-static final variable are initialized during the declaration while Local final variable can be declared only inside a method or block. Until the arrival of Java 8, the local variable was prevented from using anonymous classes but in Java 8 the local variable can be initialized only once and cannot be modified after initialization.

  • SEE: What is Java bytecode? Is it platform independent?

When you want to declare a method as final, there will be some restrictions to do it. For ex: An abstract method cannot be declared as a final in Java. The following program will explain you about to make a new final method in Java:

Class Big
{
Public final void 1surn() {
system.out.println(“No girl can’t change their Surname till marriage””);
}
}
Class babyDaughter extends Big
{
Public final void get 1surn()
{
System.out.println (“I am expecting my New surname soon”);
}
}

When this program is compiled, an error will popup which says “Cannot override a final method from Big”.

  • Read: How Copy Constructor work in java

When to Use Final modifier in Java?

The final modifier always create doubt in programmer’s mind and raises questions like when to declare a method as final in Java and when to declare a method static. The answer to both these questions is simple. If you want to prevent a method from creating any sub-classes or prevent from changing its definition, you can declare it as final. This process will prevent the class from overriding. Following these instructions might help you in deciding whether to make a method final or not:

  • Methods called from a constructor must be declared as a final because sub-classes can override and alter the purpose of the class.
  • Always make the methods used for performance critical as final because compilers inline or cache such methods.
  • If the use of a particular is over then it can be classified as final to prevent overriding.
  • Sensitive methods which have the capability to override the classes are to be declared final.
  • If you are using the template design pattern then declare the template method as final.

Final modifier is mainly used as safety design to prevent overriding of classes and the increase the compiler performance. Finals are also used to as a tool for document the program because it can be easily noted which class will maintain that particular code. If the final modifier is declared in private final method it will prevent any type of modification in subclass itself because the inner classes will binded using static binding. Other than that private final won’t give any additional value.

  • Related: How the System.out.println() is working in Java?

It can be concluded that final modifier is used as a preventive method to secure the tested code from overriding. If a method called from a constructor is not declared as final, it will result in malicious overriding from sub classes.

Filed Under: Interview Tagged With: Final Modifier, Interview, Java, Programs





Random Materials :

  • Data Mining and Data Warehousing
  • Difference Between Class and Primitive Types
  • Database Roles in SQL
  • CASE Expression in SQL
  • Concurrent Update Problem

Follow Us on Facebook

Advertisement

Company Profile

  • About Students3k
  • Contact Us
  • Earn Money Online
  • Website Archive Page
  • Privacy Policy
  • Disclaimer

Categories

  • Important Programs
  • Study Abroad
  • IT Companies
  • Career Guidance for Students
  • Teachers
  • Verbal Analogies

We Are Social

  • Facebook Fans
  • Twitter Follow
  • Google Plus
  • Pinterest Page

© Copyright 2012 - 2016 Students3k.com · All Rights Reserved · Designed By Benefits

Show