-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathChapter-10-Extra.txt
More file actions
122 lines (83 loc) · 3.72 KB
/
Chapter-10-Extra.txt
File metadata and controls
122 lines (83 loc) · 3.72 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Contents :
IS-A relationship in OOP (Inheritance)
Has-A relationship (Association)
Different type of Association : Aggregation and Composition
Example of each type
SCJP exam objective 5.5
IS-A (Inheritance) :
In Object oriented programming, IS-A relationship denotes “one object is type of another”. IS-A relation denotes Inheritance methodology.
In Java, Inheritance can be implemented with extends (in case of class) and implements(in case of interface) keywords.
A simple example of IS-A relation : Dell IS-A laptop.
Unit 2 (Prog 6) : Inheritance in JAVA contains some detailed information about inheritance.
If you will check instanceof condition “Dell instanceof Laptop”, It is true since Dell IS-A Laptop.
class Laptop {
// Code for Laptop class goes here.
}
interface Formatable {
// Members of Interface.
}
class Dell extends Laptop implements Formatable {
// More code related to Dell goes here.
// Dell class will inherit all accessible members of Laptop class.
// Dell IS-A Laptop.
// And Dells class also implements all method of Formatable interface, since
// Dell is not an abstract class.
// so Dell IS-A Formatable.
}
Inheritance Hierarchy
Inheritance hierarchy
Note that Object class will be there always on top in every inheritance Hierarchy, So every class holds IS-A relationship with Object class and instance of Object test is always true for all class.
Below image includes inheritance hierarchy for given example.
Has-A (Association) :
In Object orientation design, We can say “class one is in Has-A relationship with class B if class A holds reference of Claas B”.
By this reference of class B, A can access all properties of class B which are allowed.
Some off topic talk, To use Inheritance and Aggregation in your code is a good programming practice. It reduces the code duplication and increases reusability.
Example :
class HardDisk {
public void writeData(String data) {
System.out.println("Data is being written : " + data);
}
}
class UseDell {
// segate is referece of HardDisk class in UseDell class.
// So, UseDell Has-A HardDisk
HardDisk segate = new HardDisk();
public void save (String data) {
segate.writeData(data);
}
}
Aggregation v/s Composition :
So this is a famous confusing topic, Here we are goes to explain this as simple as possible.
Has-A relationship or Association can be divided in two types :
Aggregation and Composition
Aggregation :
Consider above example : HardDisk class and UseDell class.
In the example, UseDell HAS-A HardDisk.
If Laptop stops working somehow, you can remove harddisk from that and connect to other laptop, This is Aggregation.
In aggregation container class (UseDell) and referenced class (HardDisk) can have their independent existence.
Composition :
Now for Composition, consider two different class House and Kitchen.
class House {
Kitchen kitchen = new Kitchen();
// More code for House class.
}
class Kitchen {
// code of Kitchen class.
}
If House gets destroyed Kitchen also will be destroyed with that House, This is composition.
In composition reference class (Kitchen) can not exist if container class (House) gets destroyed.
Reference class has its own existence even if Container class gets destroyed in Aggregation.
Reference class can exist only with Container class in Composition.
public class TestRelationship {
public static void main(String[] args) {
Dell oDell = new Dell();
if (oDell instanceof Laptop) {
System.out.println("Dell IS-A Laptop.");
}
if (oDell instanceof Formatable) {
System.out.println("Dell IS-A Formatable.");
}
// Test HAS-A
new UseDell().save("This will be stored in segate.");
}
}