-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNinthCode_AdapterClass.java
More file actions
59 lines (53 loc) · 1.31 KB
/
Copy pathNinthCode_AdapterClass.java
File metadata and controls
59 lines (53 loc) · 1.31 KB
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
/*Topic-> Adapter Class:-
*
* (i) It is a simple java class that implements an interface only with empty
* implementation (body) for every method.
*
* (ii) If we implement an interface the copulsorily we should give the body for
* all the methods whether it is required or not.
*
* (iii)It is design pattern allowed to solve the problem of direct implementation of interface.
*
* eg-
*/
interface X4
{
void m11();
void m22();
void m33();
void m44();
}
class Test2 implements X4
{
//in this class we need only m3() but as we implement we need to implementation of all methods
public void m33()
{
System.out.println("m3() is executing....");
}
//rest of the we will give as empty implementation
//This approach increases the length of the code and reduces readability
public void m11() {}
public void m22() {}
public void m44() {}
}
//Solution:
abstract class AdapterX4 implements X4
{
public void m11() {}
public void m22() {}
public void m33() {}
public void m44() {}
}
class Test3 extends AdapterX4
{
public void m33()
{
System.out.println("m3() is need only...");
}
}
public class NinthCode_AdapterClass {
public static void main(String[] args) {
X4 ref=new Test3(); //loose coupling and this is a good approach
ref.m33();
}
}