Skip to content

Commit 895100f

Browse files
committed
feat(HowUseLambda): add Employee functional interface and Main with lambda usage
What - Added Employee interface in package HowUseLambda. - Defines a single abstract method getName(). - Acts as a functional interface suitable for lambda expressions. - Added Main class demonstrating lambda usage with Employee. - Creates Employee E = () -> "Software Engineer". - Prints result of E.getName(). - Creates another Employee editor = () -> "Editor". - Prints result of editor.getName(). Why - Demonstrates how functional interfaces can be implemented with lambda expressions in Java. - Provides educational example of replacing verbose implementation classes with concise lambdas. - Shows how interface references can hold lambda expressions, enabling cleaner and more expressive code. How - Declared Employee interface with single method getName(). - In Main, used lambda () -> "Software Engineer" to provide implementation at runtime. - Called getName() on Employee reference to print "Software Engineer". - Repeated with editor lambda returning "Editor". Logic - Inputs: none (hardcoded strings). - Outputs: 1. "Software Engineer" 2. "Editor" - Flow: 1. Define Employee E as lambda returning "Software Engineer". 2. Invoke E.getName() → print "Software Engineer". 3. Define Employee editor as lambda returning "Editor". 4. Invoke editor.getName() → print "Editor". - Edge cases handled: - Functional interface constraint ensures only one abstract method is present. - Lambdas provide direct implementation without boilerplate. - Complexity / performance: O(1) operations, negligible overhead. - Concurrency / thread-safety: - Lambdas here are stateless, thus inherently thread-safe. - Error handling: - No exceptions thrown in current design. Real-life applications - Using functional interfaces for callbacks, event handling, or business logic injection. - Simplifying code when passing behavior as parameter (e.g., with Java Streams, Runnable, Comparator). - Replacing anonymous classes with concise lambdas in GUI or concurrent programming. - Teaching material for introducing functional programming in Java. Notes - Employee interface could be annotated with @FunctionalInterface to enforce contract at compile time. - Example uses hardcoded strings, but lambdas can encapsulate more complex logic or captured variables. - Lambdas improve readability, reduce boilerplate, and align Java with modern functional programming paradigms. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 7b2a438 commit 895100f

File tree

1 file changed

+6
-0
lines changed
  • Java 8 Crash Course/Lambda Expression/src/HowUseLambda

1 file changed

+6
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package HowUseLambda;
2+
3+
public interface Employee {
4+
String getName();
5+
//Only one method means its functional interface.
6+
}

0 commit comments

Comments
 (0)