Skip to content
Related Articles

Related Articles

Improve Article

Can We Extend final Method in Java?

  • Last Updated : 26 May, 2021

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.

Approaches:

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.

  1. Without over-riding
  2. With over-riding 

Implementation:

  • ‘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.

Example 1



Java




// Java Program to Illsutarte Final Method Extension
  
// Importing input output classes
import java.io.*;
  
// Class 1
// Child class
// Main class
class GFG extends GFGParent {
  
    // Main driver method
    public static void main (String[] args) {
  
        // Print statement
        System.out.println("GFG!");
  
        // Creating an object og parent class in child class
        // inside the main() method
        GFGParent gfg = new GFGParent();
  
        // Calling the method created in parent class
        gfg.dataDisplay();
    }
}
  
// Helper class
// Parent class
class GFGParent {
    public final void dataDisplay () {
        System.out.println("Data Structure");
    }
}
Output
GFG!
Data Structure

Example 2

The only change that we will be making here is we are simply trying to override the method inside child class.

Java




// Java Program to Illsutarte Final Method Extension
// Where method Overrides
  
// Importing input output classes
import java.io.*;
  
// Class 1- Parent class
class GFGParent {
  
    // Method inside parent class
    public final void dataDisplay()
    {
        System.out.println("Data Structure");
    }
}
  
// Class 2
// Child class
// Main class
class GFG extends GFGParent {
  
    // Method 1
    // Created in order to illustrate overridden method/s
    public final void dataDisplay()
    {
  
        // Print statement whenever this message is called
        System.out.println("Data Structure 2");
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
  
        // Print statement
        System.out.println("GFG!");
  
        // Creating an object of parent class and
        // calling method of child class over it.
        GFGParent gfg = new GFGParent();
        gfg.dataDisplay();
    }
}

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :