Can We Extend final Method in Java?
Final method in java can be extended but alongside the main concept to be taken into consideration is that extend means that you can extend that particular class or not which is having final method, but you can not override that final method. One more case where you can not extend that final method is the class that contains the final method is also a final class.
Here we will be discussing out two approaches in order to satisfy what we just stated above to figure out how the final method is extended.
- Without over-riding
- With over-riding
- ‘GFGParent’ class is having a final method randomly named it ‘dataDisplay()’ which is created as the final method here.
- ‘GFG’ class extends GFGParent but the program executes without any error hence we can extend a final method but can not override it.
- Here if you had made the GFGParent class as final as also you will not extend the final method as we can not inherit the final classes.
GFG! Data Structure
The only change that we will be making here is we are simply trying to override the method inside child class.
Output: It generates a compile-time error.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.