Interface with Default Method

This program demonstrates interface with default method.

Problem Statement

Write a Java program to define interface Vehicle with default method fuelType().

Source Code

1 interface Vehicle {
2 void move();
3 default void fuelType(){ System.out.println("Generic fuel"); }
4 }
5 class Bike implements Vehicle {
6 public void move(){ System.out.println("Bike moves on road"); }
7 }
8 public class InterfaceDefaultMethod {
9 public static void main(String[] args) {
10 System.out.println("Eduinq Interface Default Method");
11 Vehicle v=new Bike();
12 v.move(); v.fuelType();
13 }
14 }

Program Output

Eduinq Interface Default Method
Bike moves on road
Generic fuel

Explanation

Interfaces (Java 8+) can have default methods. - Subclass inherits default unless overridden. - This enhances interface flexibility while maintaining contracts. Conclusion: Shows interface with default method.

Quick Links to Explore