Skip to content

Commit d82de2d

Browse files
committed
feat: demonstrate constructor reference using functional interface
WHAT was done: - Created a functional interface `MyConstructor` with a single abstract method `display(String str)`. - Implemented a class `ConstructerAsMethodReference` with a constructor that takes a `String` and prints it in uppercase. - Used a method reference (`ConstructerAsMethodReference::new`) to map the interface method `display()` to the class constructor. - Invoked the functional interface method to trigger object creation and execution. KEY LEARNINGS: 1. Constructor References: - Syntax: `ClassName::new` - Used to map a constructor to a functional interface method. - Acts like a bridge between a functional interface and object instantiation. 2. Functional Interface: - `MyConstructor` defines `display(String str)` as a SAM (Single Abstract Method). - Compatible with the constructor of `ConstructerAsMethodReference` which takes a `String`. 3. Flow: - `m1 = ConstructerAsMethodReference::new` → Assigns constructor reference. - `m1.display("...")` → Invokes the constructor indirectly via the functional interface. 4. Benefits: - Cleaner, more expressive code than using `new` directly inside lambda expressions. - Improves readability in functional programming and stream pipelines. REAL-LIFE APPLICATIONS: - ✅ Object creation in Java Streams (`map(ClassName::new)`). - ✅ Factory patterns where constructors can be passed as parameters. - ✅ Dependency injection frameworks to decouple object instantiation logic. RULE OF THUMB: - Use constructor references when a functional interface method matches a constructor signature. - Prefer `ClassName::new` over verbose lambdas `(str -> new ClassName(str))` for cleaner code. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent cc5aa65 commit d82de2d

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed
Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,49 @@
1-
interface MyConstructor
2-
{
3-
public void display(String str); //You can assign the more than one Parameters.
1+
interface MyConstructor {
2+
public void display(String str);
3+
//You can assign the more than one Parameters.
44
}
5+
56
public class ConstructerAsMethodReference {
6-
public ConstructerAsMethodReference(String s)
7-
{
7+
public ConstructerAsMethodReference(String s) {
88
System.out.println(s.toUpperCase());
99
}
1010

11-
public static void main(String[] args)
12-
{
11+
public static void main(String[] args) {
1312
MyConstructor m1=ConstructerAsMethodReference::new;
1413
//Constructor new along with the class name
1514
m1.display("print the upper case letters");
1615
}
17-
}
16+
}
17+
18+
/*
19+
1. Functional Interface:
20+
- `MyConstructor` ek functional interface hai (sirf ek abstract method: display).
21+
- Iska purpose hai ki us method ko kisi method reference / lambda se map kara sake.
22+
23+
2. Constructor Reference Syntax:
24+
- `ClassName::new`
25+
- Ye ek special type ka method reference hai jo constructor ko call karta hai.
26+
- Yahan `ConstructerAsMethodReference::new` ka matlab hai:
27+
-> Jab bhi `display(String s)` call hoga, ek naya object banega
28+
-> Aur us object ke constructor me wahi string pass ho jaayegi.
29+
30+
3. Flow in this program:
31+
- `MyConstructor m1 = ConstructerAsMethodReference::new;`
32+
- Matlab: "display(String)" ko map kar diya hai `ConstructerAsMethodReference(String s)` constructor pe.
33+
- Jab `m1.display("print the upper case letters");` call hota hai:
34+
→ Constructor call hoga
35+
→ String ko uppercase me convert karke print karega
36+
37+
4. Output:
38+
PRINT THE UPPER CASE LETTERS
39+
40+
5. Key Point:
41+
- Method references sirf tabhi work karte hain jab functional interface ka method signature
42+
constructor/method ke signature ke saath compatible ho.
43+
- Yahan `display(String)` perfectly match hota hai constructor `ConstructerAsMethodReference(String)` se.
44+
45+
- Constructor references ko functional programming ke saath use karke object creation ko simplify kar sakte ho.
46+
- Example: Streams me mapping:
47+
list.stream().map(ConstructerAsMethodReference::new).collect(Collectors.toList());
48+
(Har element ke liye ek naya object ban jayega.)
49+
*/

0 commit comments

Comments
 (0)