File tree Expand file tree Collapse file tree 1 file changed +79
-0
lines changed
23_Lambda_Expressions_and_Anonymous_Classes Expand file tree Collapse file tree 1 file changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ // ? Lambda Expressions
2+ // * Let us express instances of single method interfaces with lambda expressions
3+ // * Lambda expressions are anonymous functions
4+
5+ // ? Anonymous Classes
6+ // * Used to implement a base class without giving it a name.
7+
8+ interface DemoAno {
9+ void meth1 ();
10+
11+ void meth2 ();
12+ }
13+
14+ interface DemoAno2 {
15+ void meth1 ();
16+ }
17+
18+ interface DemoAno3 {
19+ void meth1 (int a );
20+ }
21+
22+ class AnonymousClass implements DemoAno {
23+
24+ public void display (){
25+ System .out .println ("Anonymous Class" );
26+ }
27+
28+ @ Override
29+ public void meth1 (){
30+ System .out .println ("I am meth1" );
31+ }
32+
33+ @ Override
34+ public void meth2 (){
35+ System .out .println ("I am meth2" );
36+ }
37+
38+ }
39+
40+ public class LambdaExpressions_and_AnonymousClasses {
41+
42+ public static void main (String [] args ){
43+
44+ // AnonymousClass ano = new AnonymousClass(); // Normal way
45+ // ano.display();
46+ // ano.meth1();
47+ // ano.meth2();
48+
49+ DemoAno obj = new DemoAno (){// Anonymous Class
50+
51+ @ Override
52+ public void meth1 (){
53+ System .out .println ("I am meth1" );
54+ }
55+
56+ @ Override
57+ public void meth2 (){
58+ System .out .println ("I am meth2" );
59+ }
60+ };
61+ obj .meth1 ();
62+ obj .meth2 ();
63+
64+ // Lambda Expressions
65+
66+ // No parameters
67+ DemoAno2 obj2 = () ->{// Only for single method interfaces (functional interfaces)
68+ System .out .println ("I am meth1" );
69+ };
70+ obj2 .meth1 ();
71+
72+ // With parameters
73+ DemoAno3 obj3 = (a ) ->{
74+ System .out .println ("I am meth1" + a );
75+ };
76+ obj3 .meth1 (5 );
77+
78+ }
79+ }
You can’t perform that action at this time.
0 commit comments