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 Override Default Method in Java?
Next article icon

Can We Override Default Method in Java?

Last Updated : 29 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Java Interface Default method is also known as Defender Method or virtual extension method.

Interfaces could have only abstract methods before Java 8. The classes separately provide implementation to these methods. So, if a new method is to be added to an interface, then its implementation code has to be provided in the class implementing the same interface. For overcoming this issue, Java 8 introduced the concept of default methods that allow the interfaces to have methods with implementation without affecting the classes that implement the interface. 

Can We Override Default Method in Java?

It is not mandatory to override the default method in Java. 

If we are using Only one interface in a Program then at a time we are using only a single default method and at that time Overriding is not required as shown in the below program:

Java
// Creating Interface interface  GfG{     public default void display() {       System.out.println("GEEKSFORGEEKS");    } }  // Main Class With Implementation Of Interface public class InterfaceExample implements GfG{    public static void main(String args[]) {       InterfaceExample obj = new InterfaceExample();             // Calling Interface       obj.display();     } } 

Output
GEEKSFORGEEKS

But when more than two Interfaces are used and both act as parent class then at that time Overriding of the Default Method is required. If we are using more than one interface and in both interfaces, if both interfaces have the same name and same structure. So at that time, one must override either one both the default method otherwise it will result in an error.

Case 1: When Two Interfaces are not overridden

Java
// Java program to demonstrate the case when  // two interfaces are not overridden  // Creating Interface One interface GfG{    public default void display() {       System.out.println("GEEKSFORGEEKS");    } }  // Creating Interface Two interface gfg{     public default void display() {       System.out.println("geeksforgeeks");    } }  // Interfaces are not Overridden  public class InterfaceExample implements GfG,gfg {    public static void main(String args[])    {       InterfaceExample obj = new InterfaceExample();       obj.display();    } } 


Output:

InterfaceExample.java:18: error: types GfG and gfg are incompatible; public class InterfaceExample implements GfG,gfg {        ^   class InterfaceExample inherits unrelated defaults for display() from types GfG and gfg 1 error

Case 2: When Two Interfaces are Overridden

Java
// Java program to demonstrate the case // when two interfaces are overridden  // Creating Interface One interface GfG{    public default void display()    {       System.out.println("GEEKSFORGEEKS");    } }  // Creating Interface Two interface gfg{     public default void display()     {       System.out.println("geeksforgeeks");    } }  public class InterfaceExample implements GfG,gfg {  // Interfaces are Overrided public void display() {          GfG.super.display();             gfg.super.display();    }  public static void main(String args[]) {       InterfaceExample obj = new InterfaceExample();       obj.display();    } } 

Output
GEEKSFORGEEKS geeksforgeeks

Next Article
Can We Override Default Method in Java?

J

jagroopofficial
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Technical Scripter 2020
  • Java 8
  • java-overriding
Practice Tags :
  • Java

Similar Reads

    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 private methods in Java?
    Let us first consider the following Java program as a simple example of Overriding or Runtime Polymorphism. Java class Base { public void fun() { System.out.println("Base fun"); } } class Derived extends Base { public void fun() { // overrides the Base's fun() System.out.println("
    4 min read
    finalize() Method in Java and How to Override it?
    The finalize() method in Java is called by the Garbage Collector just before an object is destroyed. It allows the object to perform clean-up activity. Clean-up activity means closing the resources associated with that object like Database Connection, Network Connection, or we can say resource de-al
    3 min read
    Default Methods In Java 8
    Before Java 8, interfaces could only have abstract methods. The implementation of these methods has to be provided in a separate class. So, if a new method is to be added in an interface, then its implementation code has to be provided in the class implementing the same interface. To overcome this i
    3 min read
    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
    3 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