-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterface_03.java
59 lines (50 loc) · 1.72 KB
/
Interface_03.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// abstract class Computer {
// // developer is dependent on computer, not on laptop, lets make it flexible
// // public abstract void code() {// now finally we can add abstraction
// // System.out.println();
// // }
// public abstract void code();
// }
// we can shrink this to interface
interface Computer {
void code();
} //
class Laptop implements Computer { // lets make it extend computer
public void code() {
System.out.println("code, compile, run");
}
}
class Desktop implements Computer {
public void code() {
System.out.println("code faster,compile faster,run faster");
}
}
class Developer {
// gimme laptop object then i will work
// public void devApp(Laptop lap) { // tight coupling
// // System.out.println("Coding...");
// lap.code();
// }
public void devApp(Computer cap) { // tight coupling
// System.out.println("Coding...");
cap.code();
}
}
public class Interface_03 {
public static void main(String[] args) {
// Laptop lap = new Laptop();
// Developer faisal = new Developer();
// faisal.devApp(lap); // i can not work without laptop
// // lets say we have 100 of developers we have to provide 100 laptops, that
// means
// // evertime i am creating developer object we have to create a laptop object.
// Desktop desk = new Desktop();
// Developer x = new Developer();
// // x.devApp(desk); // we are not accepting desktop objects
Computer lap = new Laptop();
Computer Desk = new Desktop();
Developer d = new Developer();
d.devApp(lap);
d.devApp(Desk);
}
}