|
| 1 | +package Java_Technical_Training; |
| 2 | + |
| 3 | +/* |
| 4 | + * Steps : |
| 5 | + * 1. create interface |
| 6 | + * 2. create class for implementation |
| 7 | + * 3. create objects |
| 8 | + * 4. use object |
| 9 | + */ |
| 10 | + |
| 11 | +@FunctionalInterface // tell it is SAM |
| 12 | +interface calc { |
| 13 | + int compute(int x, int y); |
| 14 | +} |
| 15 | + |
| 16 | +// ! first way : create class, interface and do implemention using overriding |
| 17 | +// ! second way : anonymous class(super implementation) |
| 18 | +// ! third way : lambda expression (from java 8 version ) |
| 19 | +// ? short hand style, SAM interface implementation : single abstract method |
| 20 | +// interface |
| 21 | + |
| 22 | +// class mycalc implements calc { |
| 23 | + |
| 24 | +// @Override |
| 25 | +// public int compute(int x, int y) { |
| 26 | +// return x + y; |
| 27 | +// } |
| 28 | + |
| 29 | +// } |
| 30 | +class Rparent { |
| 31 | + |
| 32 | + void show() { |
| 33 | + System.out.println("I am the R show parents"); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// ! default keyword use |
| 38 | +interface intt { |
| 39 | + void show(); |
| 40 | + |
| 41 | + default void print() { |
| 42 | + System.out.println("1. Print inside interface intt"); |
| 43 | + } |
| 44 | + |
| 45 | + static void disp() { |
| 46 | + System.out.println("Static method inside interface intt"); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +interface intt2 { |
| 51 | + default void print() { |
| 52 | + System.out.println("2. Print inside interface intt2"); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +interface intt3 extends intt, intt2 { |
| 57 | + default void print() { |
| 58 | + intt.super.print(); |
| 59 | + intt2.super.print(); |
| 60 | + System.out.println("3. This is print inside interface intt3"); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +public class InterfaceUseWays { |
| 65 | + public static void main(String[] args) { |
| 66 | + // calc obj = new mycalc(); |
| 67 | + // int res = obj.compuete(45, 54); |
| 68 | + // System.out.println(res); |
| 69 | + |
| 70 | + // ! Anonymous class |
| 71 | + // during object creation we create a class |
| 72 | + |
| 73 | + calc obj = new calc() { |
| 74 | + public int compute(int x, int y) { |
| 75 | + return x * y; |
| 76 | + } |
| 77 | + }; |
| 78 | + int r = obj.compute(54, 45); |
| 79 | + System.out.println("The result is : " + r); |
| 80 | + |
| 81 | + // ? third way |
| 82 | + // ! lambda function |
| 83 | + calc obj2 = (int a, int b) -> a + b; |
| 84 | + System.out.println("The result by lambda is : " + obj2.compute(54, 45)); |
| 85 | + |
| 86 | + // ? bytecode genrated by lamda, merges the bytecodes, hence it is an advantage |
| 87 | + // ? of using lambda function |
| 88 | + |
| 89 | + // ! Anonymous class |
| 90 | + Rparent t = new Rparent() { |
| 91 | + void show() { |
| 92 | + System.out.println("R child show..."); |
| 93 | + this.print(); |
| 94 | + } |
| 95 | + |
| 96 | + void print() { |
| 97 | + System.out.println("R child print..."); |
| 98 | + } |
| 99 | + }; |
| 100 | + t.show(); |
| 101 | + // t.print(); |
| 102 | + |
| 103 | + // ! Anonymous class |
| 104 | + intt i = new intt() { |
| 105 | + public void show() { |
| 106 | + print(); |
| 107 | + System.out.println("This is show of interface intt"); |
| 108 | + } |
| 109 | + }; |
| 110 | + i.show(); |
| 111 | + intt.disp(); |
| 112 | + System.out.println(); |
| 113 | + |
| 114 | + // ! Anonymous class |
| 115 | + intt3 ii = new intt3() { |
| 116 | + public void show() { |
| 117 | + print(); |
| 118 | + System.out.println("This is show of interface intt3"); |
| 119 | + } |
| 120 | + }; |
| 121 | + ii.print(); |
| 122 | + } |
| 123 | +} |
0 commit comments