The major difference between interfaces in Java 7 and Java 8 is that Java 8 introduced default and static methods with implementations. Before Java 8, interfaces could only define abstract methods, making them purely contractual. Java 8 enhanced interfaces to support backward compatibility and reduce the need for abstract classes.
Key Points: • Java 7 interfaces could contain only abstract methods and constants. • Java 8 introduced default methods with method implementations. • Java 8 introduced static methods inside interfaces. • These enhancements improved interface evolution without breaking existing implementations. • Functional interfaces and lambda expressions became possible in Java 8.
Java 7 Interface Features:
An interface could contain:
• Abstract methods • Public static final variables (constants)
Example:
interface Vehicle {
void start();
int MAX_SPEED = 120;
}Characteristics:
• No method implementations • No static methods • No default methods • Used only to define a contract
Java 8 Interface Features:
In addition to abstract methods, interfaces can contain:
• Default methods • Static methods • Functional interfaces support
Example:
interface Vehicle {
void start();
default void stop() {
System.out.println("Vehicle Stopped");
}
static void info() {
System.out.println("Vehicle Information");
}
}Advantages:
• Backward compatibility • Reduced code duplication • Better API evolution
Example: Suppose an interface is implemented by hundreds of classes.
In Java 7:
Adding a new method to the interface would force every implementing class to modify its code.
In Java 8:
A default implementation can be provided, preventing existing implementations from breaking.
Code Example:
interface Vehicle {
void start();
default void stop() {
System.out.println("Stopping Vehicle");
}
}
class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car Started");
}
}
public class Demo {
public static void main(String[] args) {
Car car = new Car();
car.start();
car.stop();
}
}Output:
Car Started Stopping Vehicle
Comparison Table:
Java 7 Interface:
• Abstract methods only • No method implementation • No static methods • No support for lambda expressions
Java 8 Interface:
• Abstract methods • Default methods • Static methods • Supports functional interfaces • Enables lambda expressions
Why Were Default Methods Introduced?
Consider the Java Collections Framework.
When new methods needed to be added to interfaces such as Collection or List, existing implementations would break.
Default methods solved this problem by providing a default implementation while keeping old code compatible.
Interview Tip: A concise interview answer is:
"In Java 7, interfaces could contain only abstract methods and constants. Java 8 enhanced interfaces by introducing default methods and static methods with implementations. These features improved backward compatibility, reduced code duplication, and enabled functional programming features such as lambda expressions."