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:
Default Methods In Java 8
Next article icon

Default Methods In Java 8

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 issue, Java 8 has introduced the concept of default methods which allow the interfaces to have methods with implementation without affecting the classes that implement the interface.

Java
// A simple program to Test Interface default // methods in java interface TestInterface {     // abstract method     public void square(int a);      // default method     default void show()     {       System.out.println("Default Method Executed");     } }  class TestClass implements TestInterface {     // implementation of square abstract method     public void square(int a)     {         System.out.println(a*a);     }      public static void main(String args[])     {         TestClass d = new TestClass();         d.square(4);          // default method executed         d.show();     } } 

Output:

 16  Default Method Executed 

The default methods were introduced to provide backward compatibility so that existing interfaces can use the lambda expressions without implementing the methods in the implementation class. Default methods are also known as

defender methods

or

virtual extension methods

.

Static Methods:

The interfaces can have static methods as well which is similar to static method of classes.

Java
// A simple Java program to TestClassnstrate static // methods in java interface TestInterface {     // abstract method     public void square (int a);      // static method     static void show()     {         System.out.println("Static Method Executed");     } }  class TestClass implements TestInterface {     // Implementation of square abstract method     public void square (int a)     {         System.out.println(a*a);     }      public static void main(String args[])     {         TestClass d = new TestClass();         d.square(4);          // Static method executed         TestInterface.show();     } } 

Output:

 16  Static Method Executed

Default Methods and Multiple Inheritance

In case both the implemented interfaces contain default methods with same method signature, the implementing class should explicitly specify which default method is to be used or it should override the default method.

Java
 // A simple Java program to demonstrate multiple // inheritance through default methods. interface TestInterface1 {     // default method     default void show()     {         System.out.println("Default TestInterface1");     } }  interface TestInterface2 {     // Default method     default void show()     {         System.out.println("Default TestInterface2");     } }  // Implementation class code class TestClass implements TestInterface1, TestInterface2 {     // Overriding default show method     public void show()     {         // use super keyword to call the show         // method of TestInterface1 interface         TestInterface1.super.show();          // use super keyword to call the show         // method of TestInterface2 interface         TestInterface2.super.show();     }      public static void main(String args[])     {         TestClass d = new TestClass();         d.show();     } } 

Output:

Default TestInterface1 Default TestInterface2

Important Points:

  1. Interfaces can have default methods with implementation in Java 8 on later.
  2. Interfaces can have static methods as well, similar to static methods in classes.
  3. Default methods were introduced to provide backward compatibility for old interfaces so that they can have new methods without affecting existing code.

Next Article
Default Methods In Java 8

A

Akash Ojha
Improve
Article Tags :
  • Java
Practice Tags :
  • Java

Similar Reads

    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
    Default Array Values in Java
    If we don't assign values to array elements and try to access them, the compiler does not produce an error as in the case of simple variables. Instead, it assigns values that aren't garbage. Below are the default assigned values. S. No.DatatypeDefault Value1booleanfalse2int03double0.04Stringnull5Use
    2 min read
    Method Class | getDefaultValue() Method in Java
    The getDefaultValue() method of java.lang.reflect.Method class. It returns the default value for the annotation member represented by the Method object. When annotation member belongs to primitive types, then the method returns the instance of that wrapper type. It returns null if annotation member
    3 min read
    Method Class | isDefault() Method in Java
    The java.lang.reflect.Method.isDefault() method is used to check whether the method of the Method object is a Default Method: or not. It returns true if method object is a default method, otherwise it will return false. Default Method: A public non-abstract on-static method with a body declared in a
    2 min read
    URL getDefaultPort() method in Java with Examples
    The getDefaultPort() function of URL class returns the default port of a specified URL. If the URL scheme or the URLStreamHandler for the URL do not define a default port number then the function returns -1. Function Signature public int getDefaultPort() Syntax url.getDefaultPort() Parameter This fu
    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