Skip to content

Commit 8ed7407

Browse files
committed
feat(functional-interfaces): add example of inheritance with default and static methods
What - Introduced `Parent` interface marked with `@FunctionalInterface` containing: - One abstract method → `show(String msg)`. - One default method → `defaultMethod()` for demonstration. - One static method → `staticMethod()` for utility purposes. - Created `Child` interface extending `Parent`. - Overrode `show(String msg)` but did not add new abstract methods (still valid as a functional interface). - Added `InheritanceFunctionalInterfaceExample` class: - Implemented `Child` via a lambda expression. - Showcased usage of abstract, default, and static methods in functional interfaces. Why - Demonstrates that a functional interface can extend another functional interface **as long as the total number of abstract methods remains one**. - Reinforces that **default** and **static methods** do not break the single abstract method rule. - Highlights best practices for designing reusable and backward-compatible APIs with functional interfaces. Logic 1. Define `Parent`: - `show(String msg)` → abstract method (functional interface core). - `defaultMethod()` → prints "Parent default method". - `staticMethod()` → prints "Parent static method". 2. Define `Child`: - Extends `Parent`. - Overrides `show(String msg)` but adds no new abstract methods, maintaining functional interface integrity. 3. Main class `InheritanceFunctionalInterfaceExample`: - Implements `Child` using a lambda: `msg -> System.out.println("Lambda says: " + msg)`. - Calls: - `child.show(...)` → executes lambda. - `child.defaultMethod()` → invokes inherited default implementation. - `Parent.staticMethod()` → invokes interface static method. Real-life applications - Enables extending existing functional interfaces without breaking lambda-based code. - Useful for evolving APIs with **default helpers** (logging, validation) while preserving functional style. - Foundational pattern in Java standard library (`java.util.function` interfaces like `Function`, `Consumer`). - Helps create clean, expressive APIs that balance **functional programming** with **object-oriented design**. Notes - `@FunctionalInterface` enforces compile-time safety ensuring only one abstract method exists. - If multiple abstract methods are added, the interface ceases to be functional, breaking lambda support. - Default methods → can be overridden in implementing classes. - Static methods → must be called via interface name, not via object reference. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 403d274 commit 8ed7407

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@FunctionalInterface
2+
interface Parent {
3+
// Single abstract method.
4+
void show(String msg);
5+
6+
// Default method → does not count towards abstract method count.
7+
default void defaultMethod() {
8+
System.out.println("Parent default method");
9+
}
10+
11+
// Static method → does not count either.
12+
static void staticMethod() {
13+
System.out.println("Parent static method");
14+
}
15+
}
16+
17+
// Child interface inherits Parent.
18+
@FunctionalInterface
19+
interface Child extends Parent {
20+
/*
21+
Do NOT add another abstract method here, otherwise it will break functional interface rules.
22+
You can override the same abstract method if you want.
23+
*/
24+
@Override
25+
void show(String msg); // same method from Parent
26+
}
27+
28+
public class InheritanceFunctionalInterfaceExample {
29+
public static void main(String[] args) {
30+
// Using lambda expression for Child (inherited method `show`)
31+
Child child = (msg) -> System.out.println("Lambda says: " + msg);
32+
33+
// Call the abstract method
34+
child.show("Hello Inheritance in Functional Interfaces!");
35+
36+
// Call the default method
37+
child.defaultMethod();
38+
39+
// Call the static method (through Parent interface)
40+
Parent.staticMethod();
41+
}
42+
}

0 commit comments

Comments
 (0)