final method in java

In java final keyword is one of the most important concepts. Many programmers may have a lot of doubts in mind. I have heard may time some common questions about final keyword like How to make a final method? When to make a method final ? What is the need to make a final method?We have already covered final variable in java in a sperate topic. In this topic we will learn all about the final method.  

Here is the table content of the article will we will cover this topic.
1. What is the final method in Java?
2. How to use the final method?

3. Important points about the final method
4. When to use the final method?

What is the final method in Java?

final keyword can be applied to a variable, method, and class.  I have seen in the previous post how the final variable used in java and apply some restriction to the user.  When we use the final keyword with the method, JVM applies restriction to the method.   

We can declare a method in a class with the final keyword. A method declared with the final keyword is called the final method. A final method can’t be overridden in subclasses. Whenever we define a final keyword with a method, the final keyword indicates to the JVM that the method should not be overridden by subclasses.

In java, there are many classes that have final methods and they are not overridden in subclasses. In java Object class have notify() and wait() method are final methods. The final method enforces the subclasses to follow the same implementation throughout all the subclasses.

NOTE: The final method always binds at compile time in java. It is much faster than non-final methods because they are not required to be resolved during run-time.

accessModifier final returnType methodName()
{
    //Body of method
}

Here accessModifier specify the scope of method. You can read about access modifiers.
final is a keyword that is used to make a final method.
returnType is the type of data which you want to return from method.
methodName is the name of method.

public  final void showData()
{
       // Body of method
}

How to use the final method

We must declare methods with the final keyword if we required to follow the same implementation throughout all the derived classes. By use of the final method, we can apply the restriction in inheritance.

class ParentClass 
{
    final void showData() 
    {
        System.out.println("This is a final method of Parent class");
    }
}

class ChildClass extends ParentClass 
{
	// It will throw compliation error
	final void showData() 
    {
        System.out.println("This is a final method of Child class");
    }
}
class MainClass
{
	public static void main(String arg[])
	{
		ParentClass obj = new ChildClass();
		obj.showData();
	}
}

Output: Compilation error

Important points about final method

final method with the private keyword

A private and final should not use together with method because it doesn’t make any sense. Because private methods of superclass are not inherited in subclasses and they act like a final method.  

final method with the static keyword

As you already know we can’t override a static method in subclasses. If we try to override a static method, then it is known as method hiding. So, we should not make the static final method in java. Because both keywords doesn’t create a value together.

final method with abstract keyword

A method declared without a body is an abstract method.  We declare the abstract method only when we require just method declaration in Parent classes and implementation will provide by child classes. We can’t use the final keyword with abstract methods because both concepts contradict each other.

Performance

The final method always binds at compile time. The compiler knows that final methods can’t be overridden in derived classes. These methods provide better performance than nonfinal methods.

When to use the final method?

When you want to apply restriction on the method. It means that no one can’t change the definition of the final method in the derived classes. Let’s say you want to provide any important or secure information in the method so you can make it as the final method. So that any derived class can’t override it or manipulate the information of method.

Lets take an example of sensitive data of office. In office there may be some data that are not accessible by each employee. So these type of data should be restricted.

class OfficeData 
{ 
	void policy()
	{
		System.out.println("There are three policies");
	}
	final void financial()
	{
		System.out.println("Offic financial data");
	}
}
public class Record extends OfficeData
{
	public void mainRecord()
	{
		System.out.println("All data record of office");
	}
	@Override
	void policy()
	{
		System.out.println("Adding one more policy");
	}
	
	public static void main(String arg[])
	{
		Record record = new Record();
		record.mainRecord();
		record.policy();
	}
}

Output: All data record of office
Adding one more policy

final method in java

Leave a Comment