Chapter | Java 8 |
Author | Ayush Shrivastava |
Hub | Java Backend Development: Zero to hero |
Java 8 was one of the most important releases in the history of Java. It introduced a lot of new features that completely changed the way we write code. The main aim of Java 8 is
Java 8 brought powerful new features that made coding in Java simpler and more expressive. Features like default methods, static methods, and functional interfaces allow developers to write cleaner and more flexible code. Understanding these concepts is the first step toward mastering functional programming in Java.
To simplify programming
To enable functional programming
To write more readable and concise code
Let us go through the major features of Java 8 one by one.
Why Java 8 Was a Game Changer
Before Java 8, Java was considered more of a pure object-oriented language. Writing concise code for collections, data manipulation, or functional-style operations was often verbose and repetitive. Developers relied heavily on anonymous classes, which made the code harder to read.
Java 8 solved these problems by introducing functional programming concepts into Java, while still keeping backward compatibility.
New Features in Java 8
Interface changes (Default and Static methods)
Lambda Expressions
Functional Interfaces
Consumer
Supplier
Predicate
Function
Stream API
Date & Time API changes
Optional class
Spliterator
StringJoiner
Method References
Constructor References
Changes in Collections Framework
Interface Changes in Java 8
Before Java 8, interfaces could only contain abstract methods (methods without a body). But from Java 8 onwards, interfaces can also have default and static methods.
Why Default and Static Methods?
The main advantage is backward compatibility.
Imagine you have an interface Test
that is implemented by many classes (A, B, C, D). If you add a new method to the Test
interface, then every class that implements it must also implement this new method. This becomes very difficult if hundreds of classes implement the interface.
Default Method in Interface
package ayshiv;
interface Vehicle {
void start();
// Default method
default void fuel() {
System.out.println("Refueling the vehicle...");
}
}
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car is starting...");
}
}
public class MasteringBackend {
public static void main(String[] args) {
Vehicle car = new Car();
car.start();
car.fuel(); // using default method
}
}
Key Points about Default Methods:
Must have a body.
Implicitly
public
.Classes implementing the interface are not required to implement them.
Can be overridden if needed.
Static Methods in Interfaces
Static methods in interfaces are also allowed in Java 8. The difference is that static methods cannot be overridden. They belong to the interface itself and must be called using the interface name.
Static Method in Interface
package ayshiv;
interface Vehicle {
static void cleanVehicle() {
System.out.println("Cleaning the vehicle...");
}
}
public class MasteringBackend {
public static void main(String[] args) {
// Static method called using interface name
Vehicle.cleanVehicle();
}
}
Key Points about Static Methods:
Must have a body.
Implicitly
public
.Called using the interface name.
Cannot be overridden.
Functional Interfaces
A Functional Interface is an interface that contains only one abstract method. Functional interfaces are used to invoke Lambda Expressions.
Some predefined functional interfaces in Java are:
Runnable
→ run()Callable
→ call()Comparable
→ compareTo()ActionListener
→ actionPerformed()
We can also create our own functional interfaces using the @FunctionalInterface
annotation.
Functional Interface with Lambda Expression
package ayshiv;
// Functional Interface
@FunctionalInterface
interface MyFunctionalInterface {
void greet(String name);
}
public class MasteringBackend {
public static void main(String[] args) {
// Using Lambda Expression
MyFunctionalInterface greeting = (name) -> System.out.println("Hello, " + name);
greeting.greet("Ayush");
}
}
Key Points about Functional Interfaces:
Can contain only one abstract method.
Can have any number of default or static methods.
Used to represent Lambda expressions.
Foundation for Functional Programming
Together, default methods, static methods, and functional interfaces laid the foundation for functional programming in Java.
Functional programming makes code more declarative (what to do, not how to do it).
These features are closely tied with the Stream API (introduced in Java 8), which allows operations like
map,
filter,
andreduce
to be performed on collections in a clean, chainable style.