Final Method in Java
Learn via video course
Overview
When we want that the content of a method should not change by any outsider or child class, then we declare the method as the final method in java. When a method is declared as the final method in the parent class, then any child class cannot override or modify the final method in java. Whenever we define any variable or class or method as final, then the particular entity cannot be changed after it has been assigned.
Scope
The article contains topics such as
- What is the use of the final keyword.
- What is a final method in java.
Each of the topics is explained clearly with diagrams and examples wherever necessary.
What is the final method in Java?
Before learning about the final method in java, let us first know about the final keyword in java. The final keyword is used to denote the constants. The final keyword is usually used with methods, classes, and variables. Whenever we define any variable or class, or method as final, then the particular entity cannot be changed after it has been assigned. In simple terms, we can say that the final variable cannot be reinitialized or overridden. In the case of the final class, the final class cannot be extended.
:::{.tip} Note: Inheritance is one of the key features of Object Oriented Programming that allows us to create a new class (child class) from an existing class (parent class).
Whenever we want that the content of a method should not change by any outsider or child class, then we declare the method as the final method in java. The final keyword before the name of a method indicates that the method cannot be overridden by subclasses. When a method is declared as the final method in the parent class, then any child class cannot override or modify the final method in java.
The idea or purpose of creating the final methods is to restrict the unwanted and improper use of method definition while overriding the parent class method. Since overriding a method may change the meaning of the method, the user might interpret it wrongly. Therefore, in some cases where the developer does not want any kind of changes in some particular functionality, he/she declares such methods as final methods in java. Let us take a real-life example to see the problem of not using a final method in java.
Suppose we make a class Car and declare a non-final method wheels() in it. If someone creates a Skoda class or some other car company class and overrides the wheels() method in it and changes the number of the wheels to something odd, like 5 or 7 or 3, then the actual structure of the car is changed. This overriding should not be done in the structure defined by the main developer. This will make the wrong use of our method, so to overcome this situation, we use the final method.
final method in java with example
As we have seen, the final method in java is the method that cannot be changed or overridden once defined. Hence, we cannot modify a final method from the subclass.
Let us take an example to understand the working and usage of the final method in java.
Output:
Since we have tried to change the final method of the parent class inside the child class i.e., tried to override the final method in java, we got a compilation error. The compilation error itself states that the dispaly() method inside the child class, i.e., the Test class, cannot override the display() method of the parent class i.e. FinalClassExample class, because the overridden display() method is a final method.
section{.main}
Important Points
- When a method is declared with the final keyword, it is called a final method in java. We cannot override the final method in java.
- If we try to override the final method in java, we get a compilation error because if we are declaring any method with the final keyword, we are indicating the JVM to provide some special attention and make sure no one can override it.
- The methods that are required to follow the same implementation throughout all the derived classes must be declared as a final method.
- We define a final method in java to restrict the unwanted and improper use of method definition by the child class(es) while overriding the final method.
:::
Conclusion
- When we want that the content of a method not to be changed by any outsider or child class, then we declare the method as the final method in java.
- Whenever we define any variable or class, or method as final then the particular entity cannot be changed after it has been assigned.
- The final variable cannot be reinitialized or overridden. In the case of the final class, the final class cannot be extended.