Skip to content

Commit 51210fa

Browse files
committed
feat(LambdaExpressionHard): add demo combining FunctionalInterface, class, and lambdas
What - Added MathOperation functional interface with single abstract method operate(int, int). - Implemented SumOperation class: - Overrides operate() by delegating to add(). - Provides additional add() and multiply() methods. - Added LambdaExpressionHard class demonstrating: - Lambda for addition: (a, b) -> a + b. - Lambda for subtraction: (a, b) -> a - b. - Usage of helper method processOperation() to apply operations. - Example with SumOperation instance performing add() and multiply(). Why - Illustrates difference between traditional class-based implementation and modern lambda-based functional programming. - Demonstrates how @FunctionalInterface enforces Single Abstract Method (SAM) contract. - Provides a practical example of reusable operations (mini calculator) implemented with both OOP and FP approaches. - Shows how lambdas improve conciseness compared to verbose class definitions. How - Defined @FunctionalInterface MathOperation with method operate(). - Created SumOperation class implementing MathOperation and extending functionality with add() and multiply(). - In main(): - Created lambdas for addition and subtraction. - Executed them with test inputs a = 10, b = 5. - Passed lambdas into private helper processOperation() for internal use. - Instantiated SumOperation and demonstrated add() and multiply(). - Kept processOperation private to restrict usage within class scope. Logic - Inputs: integers a = 10, b = 5. - Outputs (available internally for further processing): - Lambda addition → 15. - Lambda subtraction → 5. - SumOperation.add(10, 5) → 15. - SumOperation.multiply(10, 5) → 50. - Flow: 1. Create lambdas for addition/subtraction. 2. Execute lambdas directly and via helper method. 3. Instantiate SumOperation class. 4. Call add() and multiply() for results. - Edge cases: - Lambdas work only because MathOperation has single abstract method. - multiply() is an extension beyond interface contract, showing class flexibility. - Complexity / performance: O(1) per operation, negligible. - Concurrency / thread-safety: - Lambdas are stateless and thread-safe. - SumOperation methods are stateless, safe across threads. - Error handling: - No exceptions expected for basic arithmetic. - Overflow possible with large integers, inherent to int. Real-life applications - Calculator-style applications mixing OOP and functional programming. - Replacing strategy pattern classes with lambdas for concise logic. - Passing operations as parameters to methods for higher-order function usage. - Educational example contrasting pre-Java-8 and Java-8+ approaches. Notes - processOperation demonstrates reusability by accepting any MathOperation implementation (class or lambda). - @FunctionalInterface annotation ensures compiler enforces single abstract method constraint. - For larger systems, prefer lambdas for simple inline logic and classes for complex, reusable implementations. - Example illustrates dual paradigms: OOP extensibility (SumOperation) and FP conciseness (lambdas). Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 9e09c71 commit 51210fa

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

JAVA8/LambdaExpression/src/LambdaExpressionHard.java renamed to Java 8 Crash Course/Lambda Expression/src/LambdaExpressionHard.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ interface MathOperation {
55

66
// Making the implementing class public so it can be used outside its package if needed.
77
class SumOperation implements MathOperation {
8-
98
@Override
109
public int operate(int a, int b) {
1110
// Delegate to the add method.
@@ -22,7 +21,6 @@ public int multiply(int a, int b) {
2221
}
2322

2423
public class LambdaExpressionHard {
25-
2624
public static void main(String[] args) {
2725
// Lambda expression for addition (explicit parameter types can be omitted due to type inference)
2826
MathOperation addOperation = (a, b) -> a + b;
@@ -54,3 +52,43 @@ private static void processOperation(int a, int b, MathOperation operation) {
5452
// The result can be used internally without printing to the console.
5553
}
5654
}
55+
56+
/*
57+
1. @FunctionalInterface:
58+
- Ek interface jisme sirf ek abstract method hota hai.
59+
- `MathOperation` ke andar `operate(int a, int b)` method define hai.
60+
- Isko lambda expressions ke saath use kiya ja sakta hai.
61+
62+
2. SumOperation Class:
63+
- `implements MathOperation` → means interface ka contract follow karna.
64+
- `operate(a, b)` ko override kiya aur andar `add()` method ko call karaya.
65+
- Additional methods bhi banaye:
66+
✔ add(a, b) → numbers ko add karega.
67+
✔ multiply(a, b) → numbers ko multiply karega.
68+
69+
3. Lambda Expressions:
70+
- `MathOperation addOperation = (a, b) -> a + b;`
71+
→ addition ke liye ek inline implementation (lambda).
72+
- `MathOperation subtractOperation = (a, b) -> a - b;`
73+
→ subtraction ke liye lambda.
74+
75+
✔ Lambdas = concise alternative instead of creating full classes.
76+
77+
4. Main Logic:
78+
- a = 10, b = 5
79+
- `addOperation.operate(a, b)` → 15
80+
- `subtractOperation.operate(a, b)` → 5
81+
- `SumOperation sumOp.add(a, b)` → 15
82+
- `SumOperation sumOp.multiply(a, b)` → 50
83+
84+
5. processOperation Method:
85+
- Ek helper method jo `MathOperation` ko input leta hai.
86+
- Isme operation performs hota hai aur result internally handle hota hai.
87+
- Private rakha hai, kyunki sirf is class ke andar kaam karna hai.
88+
89+
✔ FunctionalInterface = single abstract method (SAM).
90+
✔ Lambda = short & clean way to implement functional interfaces.
91+
✔ `SumOperation` class → traditional way, Lambda → modern concise way.
92+
✔ Helper method `processOperation()` dikhata hai kaise reusable lambdas pass karke operations handle karte ho.
93+
✔ Ye program ek mini calculator example hai using both OOP (class) and FP (lambda).
94+
*/

0 commit comments

Comments
 (0)