Open In App

Can We Extend final Method in Java?

Last Updated : 19 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 Illustrate 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 of 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 Illustrate 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads