Nearby lessons

93 of 125

Java - Default and Static Methods in Interfaces

📌 What You Will Learn
  • Lambda expressions — the big change
  • Functional interfaces
  • Default and static methods in interfaces
  • The four workhorses: Predicate, Function, Consumer, Supplier
  • Method references (::)
  • The Stream API for processing collections
  • Optional — safe null handling
  • The new Date and Time API

Default and Static Methods in Interfaces is a core concept of the Java language. This lesson explains Default and Static Methods in Interfaces with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Default and Static Methods in Interfaces

Before Java 8, interfaces could have only method declarations. Java 8 added default methods (with a body) so interfaces could grow without breaking all the classes that implement them.

This is how Java 8 added new features (like Stream) to old collections without changing all the existing collection classes.

Example01
JCode Cell
1interface Vehicle {
2 void start(); // abstract - implementer must provide
3 
4 default void horn() { // default method - has a body
5 System.out.println("Beep beep!");
6 }
7 
8 static void info() { // static method in interface
9 System.out.println("This is a vehicle");
10 }
11}
12 
13class Car implements Vehicle {
14 public void start() {
15 System.out.println("Car started");
16 }
17 // horn() is inherited as-is
18}
19 
20class Demo {
21 public static void main(String[] args) {
22 Car c = new Car();
23 c.start();
24 c.horn(); // default method used directly
25 Vehicle.info(); // static method via interface name
26 }
27}
Output
Car started Beep beep! This is a vehicle
📝 Key Takeaways
  • Lambda = a function without a name; (params) -> logic.
  • Functional interface = one abstract method, so a lambda can implement it.
  • Interfaces now have default and static methods with bodies.
  • Predicate (test), Function (apply), Consumer (accept), Supplier (get) are the four core functional interfaces.
  • Method references (::) are shorthand for lambdas.
  • Streams process collections with filter/map/sorted/collect pipelines.
  • Optional prevents NullPointerException with orElse/isPresent.
  • java.time (LocalDate, LocalDateTime) replaces the old Date/Calendar cleanly.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1