Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Can We Extend final Method in Java?
Next article icon

Can We Extend final Method in Java?

Last Updated : 19 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
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.


Next Article
Can We Extend final Method in Java?

R

rajatagrawal5
Improve
Article Tags :
  • Java
  • Java-final keyword
Practice Tags :
  • Java

Similar Reads

    Private and Final Methods in Java
    Methods in Java play an important role in making the code more readable, support code reusability, and defining the behaviour of the objects. And we can also restrict the methods based on requirements using the keywords and modifiers such as final and private. These two have different, distinct purp
    5 min read
    Can We Override Final Method in Java?
    A method in a subclass is said to Override a method in its superclass if that method has a signature identical to a method in its superclass. When this method is called by an object of the subclass, then always the Overridden version will be called. In other words, the method which was Overridden in
    3 min read
    Can We Override Default Method in Java?
    Default method in Java is a method in java which are defined inside the interface with the keyword default is known as the default method. It is a type of non-abstract method. This method is capable of adding backward capability so that the old interface can grasp the lambda expression capability. J
    3 min read
    Java Extension Methods
    In object-oriented computer programming, an extension method is a method added to an object after the original object was compiled. Extension methods are features of some object-oriented programming languages. There is no syntactic difference between calling an extension method and calling a method
    4 min read
    Calendar complete() Method in Java with Examples
    The complete() method in Calendar class is used to fill any unset fields of the calendar fields. It follows the following process of completion: At first, if the time value has not been calculated, the computeTime() method is called to calculate it from calendar field values. Second, to calculate al
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences