Java
Module 10: Abstraction & InterfacesBeginner25 min read

Abstract Classes, Interfaces & Functional Interfaces

Hide implementation complexity with abstract classes and interfaces, implement multiple interfaces, and use Java 8+ default/static interface methods.

Key Takeaways & Core Concepts
  • `default` methods (Java 8+): Allow adding new methods to interfaces without breaking existing implementations.
  • `@FunctionalInterface`: Marks an interface with exactly one abstract method so it can be implemented concisely via a Lambda expression or Method Reference.

Introduction

Abstraction separates what a component does (its public contract) from how it does it (its internal implementation). In Java, abstraction is achieved with abstract classes and interfaces.

Interface with Default Method & Lambda Implementation
1interface Notification {
2 void send(String message);
3 default void log(String message) {
4 System.out.println("[LOG] " + message);
5 }
6}
7
8@FunctionalInterface
9interface Formatter {
10 String format(String input);
11}
12
13public class Main {
14 public static void main(String[] args) {
15 Formatter upper = s -> s.trim().toUpperCase();
16 Notification n = msg -> System.out.println("Alert: " + upper.format(msg));
17 n.send(" welcome to codezov java ");
18 n.log("Dispatched");
19 }
20}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
20 lines
Note: Because `Notification` has only one abstract method (`send`), it can also be implemented as a lambda while still inheriting `default void log(...)`.

Abstract Class vs Interface

FeatureAbstract ClassInterface
InstantiationCannot be instantiatedCannot be instantiated
MethodsAbstract + concrete + constructorsAbstract, default, static, private (Java 9+)
FieldsAny visibility, mutable or finalpublic static final constants only
InheritanceSingle (extends)Multiple (implements A, B)
When to UseShared state & base behavior across closely related classesCapability contracts across unrelated classes (e.g., Comparable, Serializable)

Default, Static & Functional Interfaces

  • `default` methods (Java 8+): Allow adding new methods to interfaces without breaking existing implementations.
  • `@FunctionalInterface`: Marks an interface with exactly one abstract method so it can be implemented concisely via a Lambda expression or Method Reference.
Practice & AssessmentJava Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)