Skip to content

Commit 44ab9a8

Browse files
authored
21/03
1 parent 76eaf2c commit 44ab9a8

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed

training/InterfaceDemo.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package Java_Technical_Training;
2+
3+
/*
4+
* What is interface, why do we need it??
5+
* it is a keyword in java, used to create standards or interfaces.
6+
* it provides 100 % abstraction
7+
*
8+
*/
9+
10+
interface DBOperation {
11+
// ! 100 % abstract and public class
12+
13+
int MAX_CONNECTIONS = 10;
14+
// ? public static final int MAX_CONNECTIONS = 10;
15+
// MAX_CONNECTIONS=20; // ? can't update this variable
16+
17+
void add();
18+
19+
void remove();
20+
21+
void update();
22+
23+
void read();
24+
}
25+
26+
class OracleDB implements DBOperation {
27+
28+
@Override
29+
public void add() {
30+
// MAX_CONNECTIONS = 20;
31+
System.out.println("This is add operation ");
32+
}
33+
34+
@Override
35+
public void read() {
36+
System.out.println("This is read operation ");
37+
38+
}
39+
40+
@Override
41+
public void update() {
42+
System.out.println("This is update operation ");
43+
}
44+
45+
@Override
46+
public void remove() {
47+
System.out.println("This is remove operation ");
48+
49+
}
50+
51+
}
52+
53+
public class InterfaceDemo {
54+
public static void main(String[] args) {
55+
OracleDB obj = new OracleDB();
56+
obj.add();
57+
obj.read();
58+
obj.update();
59+
obj.remove();
60+
}
61+
62+
}

training/InterfaceUseWays.java

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)