GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • class inherits
  • interfaces
  • Introduction3 min read
  • Variables2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Default Methods In Java 8 - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Default Methods In Java 8

Last Updated : 11 Sep, 2026

Java 8 introduced default methods to allow interfaces to contain methods with predefined implementations. This feature makes it easier to extend existing interfaces without forcing all implementing classes to add the new method.

  • A default method is declared using the default keyword and contains a method body.
  • An implementing class inherits the default implementation unless it overrides the method.
  • When two interfaces provide the same default method, the implementing class must resolve the conflict.

Example: In this program, the GFG class uses the default service() method provided by the Vehicle interface.

Java
interface Vehicle {
    void start();

    default void service() {
        System.out.println("Vehicle service is required");
    }
}

public class GFG implements Vehicle {

    @Override
    public void start() {
        System.out.println("Vehicle started");
    }

    public static void main(String[] args) {
        GFG obj = new GFG();

        obj.start();
        obj.service();
    }
}

Output
Vehicle started
Vehicle service is required

Explanation: Since service() already has an implementation, GFG uses it directly without overriding the method.

Syntax

default returnType methodName(){
// implementation
}

Resolving Default Method Conflicts

When multiple interfaces provide default methods with the same signature, Java requires the implementing class to resolve the conflict by overriding the method or explicitly selecting a specific interface implementation.

1. Default Method Conflict

A conflict occurs when a class implements two interfaces that contain default methods with the same name and signature. The class must override the method to provide its own implementation.

Java
interface Printer {
    default void print() {
        System.out.println("Printing from Printer");
    }
}

interface Scanner {
    default void print() {
        System.out.println("Printing from Scanner");
    }
}

public class GFG implements Printer, Scanner {

    @Override
    public void print() {
        System.out.println("Custom print method");
    }

    public static void main(String[] args) {
        GFG obj = new GFG();
        obj.print();
    }
}

Output
Custom print method

Explanation: Since both interfaces define print(), the class resolves the ambiguity by overriding the method.

2. Calling a Specific Default Method

A class that overrides a conflicting default method may also call a particular interface's implementation using the InterfaceName.super.methodName() syntax.

Java
interface Printer {
    default void print() {
        System.out.println("Printer interface");
    }
}

interface Scanner {
    default void print() {
        System.out.println("Scanner interface");
    }
}

public class GFG implements Printer, Scanner {

    @Override
    public void print() {
        Printer.super.print();
        Scanner.super.print();
    }

    public static void main(String[] args) {
        GFG obj = new GFG();
        obj.print();
    }
}

Output
Printer interface
Scanner interface

Explanation: Printer.super.print() and Scanner.super.print() select the required interface implementations.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.