0Pricing
Java Academy · Lesson

Custom Functional Interfaces

Define your own with @FunctionalInterface.

Custom Functional Interfaces is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Makes an Interface Functional

A functional interface has exactly one abstract method (SAM). That single method is what a lambda implements.

  • Default and static methods do not count against the limit.
interface Greeter {
    String greet(String name);
}

public class Main {
    public static void main(String[] args) {
        Greeter g = name -> "Hello, " + name;
        System.out.println(g.greet("Ada"));
    }
}

The @FunctionalInterface Annotation

@FunctionalInterface tells the compiler to enforce the single-abstract-method rule. If you accidentally add a second abstract method, compilation fails.

@FunctionalInterface
interface Calculator {
    int compute(int a, int b);
}

public class Main {
    public static void main(String[] args) {
        Calculator add = (a, b) -> a + b;
        System.out.println(add.compute(3, 4));
    }
}

Why the Annotation Helps

The annotation documents intent and protects the contract. Without it the interface still works as functional, but a teammate could break it by adding another abstract method.

@FunctionalInterface
interface Transformer {
    String apply(String input);
}

public class Main {
    public static void main(String[] args) {
        Transformer reverse = s -> new StringBuilder(s).reverse().toString();
        System.out.println(reverse.apply("abc"));
    }
}

Custom Interface with Generics

Your interface can be generic, just like the built-in ones.

@FunctionalInterface
interface Converter<S, T> {
    T convert(S source);
}

public class Main {
    public static void main(String[] args) {
        Converter<String, Integer> toInt = Integer::parseInt;
        System.out.println(toInt.convert("123") + 1);
    }
}

Default Methods Are Allowed

A functional interface may have default methods. They add behavior without breaking the single-abstract-method rule.

@FunctionalInterface
interface Operation {
    int apply(int x);

    default Operation then(Operation next) {
        return x -> next.apply(this.apply(x));
    }
}

public class Main {
    public static void main(String[] args) {
        Operation inc = x -> x + 1;
        Operation dbl = x -> x * 2;
        System.out.println(inc.then(dbl).apply(5));
    }
}

Static Methods Are Allowed Too

Static methods can act as factories, similar to Function.identity().

@FunctionalInterface
interface Mapper {
    int map(int x);

    static Mapper identity() {
        return x -> x;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println(Mapper.identity().map(99));
    }
}

Throwing Checked Exceptions

If your abstract method declares a checked exception, the lambda may throw it. The built-in Function cannot do this, which is one reason to define your own.

@FunctionalInterface
interface RiskyParser {
    int parse(String s) throws NumberFormatException;
}

public class Main {
    public static void main(String[] args) {
        RiskyParser p = Integer::parseInt;
        System.out.println(p.parse("77"));
    }
}

Passing Custom Interfaces to Methods

Your interface becomes a parameter type, letting callers supply behavior via lambdas.

@FunctionalInterface
interface IntCombiner {
    int combine(int a, int b);
}

public class Main {
    static int reduce(int[] arr, IntCombiner c) {
        int acc = arr[0];
        for (int i = 1; i < arr.length; i++) acc = c.combine(acc, arr[i]);
        return acc;
    }

    public static void main(String[] args) {
        int[] data = {1, 2, 3, 4};
        System.out.println(reduce(data, (a, b) -> a + b));
    }
}

Three-Argument Interface

The JDK only ships single- and bi-argument interfaces. For three or more arguments, define your own.

@FunctionalInterface
interface TriFunction<A, B, C, R> {
    R apply(A a, B b, C c);
}

public class Main {
    public static void main(String[] args) {
        TriFunction<Integer, Integer, Integer, Integer> sum3 = (a, b, c) -> a + b + c;
        System.out.println(sum3.apply(1, 2, 3));
    }
}

Method References Fit Custom Interfaces

Any method whose signature matches the SAM can be supplied as a method reference.

@FunctionalInterface
interface StringTest {
    boolean check(String s);
}

public class Main {
    public static void main(String[] args) {
        StringTest blank = String::isBlank;
        System.out.println(blank.check("   "));
    }
}

Anonymous Class vs Lambda

A lambda is a concise replacement for an anonymous class implementing a functional interface. Both produce the same behavior.

@FunctionalInterface
interface Action {
    void run();
}

public class Main {
    public static void main(String[] args) {
        Action old = new Action() {
            public void run() { System.out.println("anonymous"); }
        };
        Action neo = () -> System.out.println("lambda");
        old.run();
        neo.run();
    }
}

Quick Check

What does @FunctionalInterface guarantee?

Recap

You defined your own functional interfaces:

  • A functional interface has exactly one abstract method (SAM).
  • @FunctionalInterface enforces that rule at compile time.
  • Default and static methods, generics, and checked exceptions are all allowed.
  • Custom interfaces fill gaps the JDK does not cover, such as three-arg functions or checked-exception lambdas.

Frequently asked questions

Is the “Custom Functional Interfaces” lesson free?

Yes — the full text of “Custom Functional Interfaces” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Custom Functional Interfaces”?

Define your own with @FunctionalInterface. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Custom Functional Interfaces” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Function and BiFunction
  2. Supplier and Consumer
  3. Predicate and Composition
  4. Custom Functional Interfaces
← Back to Java Academy