Java Topics
Default aur Static Methods in Interface
Last Updated : 26 May, 2026
title: Default and Static Methods in Interface
title: Default and Static Methods in Interface description: Java 8 mein interface ke default aur static methods
Java 8 se interfaces mein concrete methods add ho sakte hain — backward compatibility ke liye.
Default Method
interface Vehicle {
void start(); // Abstract
// Default method — implementation hai
default void stop() {
System.out.println("Vehicle ruk gayi");
}
default void fuelUp() {
System.out.println("Fuel bhara ja raha hai");
}
}
class Car implements Vehicle {
@Override
public void start() { System.out.println("Car start hui"); }
// stop() override nahi kiya — Vehicle ka default use hoga
// fuelUp() override kiya
@Override
public void fuelUp() {
System.out.println("Car mein petrol bhara");
}
}
Car c = new Car();
c.start(); // "Car start hui"
c.stop(); // "Vehicle ruk gayi" (default)
c.fuelUp(); // "Car mein petrol bhara" (overridden)Diamond Problem Resolution
interface A { default void hello() { System.out.println("A"); } }
interface B { default void hello() { System.out.println("B"); } }
class C implements A, B {
@Override
public void hello() {
A.super.hello(); // Explicitly choose A's version
B.super.hello(); // Or B's version
}
}Static Method
interface MathUtils {
static int square(int n) { return n * n; }
static int cube(int n) { return n * n * n; }
static boolean isPositive(int n) { return n > 0; }
}
// Interface name se call karo
MathUtils.square(5); // 25
MathUtils.isPositive(3); // trueKyun Add Kiya Gaya?
- Existing interfaces mein methods add karna — implementing classes tod nahi jaaye
- Utility methods interface ke paas rakhna
- Collection interfaces mein
forEach,stream,removeIfetc. add kiya gaya
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Default aur Static Methods in Interface.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java topic.
Search Terms
java, java programming, core java, java master course, java notes, master, course, features
Related Java Topics