1
- /*
2
- Composition Part 1
1
+ /**
2
+ * Composition Part 1
3
+ *
4
+ * @author Chirag Singhal
5
+ */
3
6
7
+ class Product {
8
+ private String model ;
9
+ private String Manufacturer ;
10
+ private int width ;
11
+ private int height ;
12
+ private int depth ;
13
+
14
+ public Product (String model , String manufacturer ) {
15
+ this .model = model ;
16
+ Manufacturer = manufacturer ;
17
+ }
18
+
19
+ }
20
+
21
+ class Monitor extends Product {
22
+ private int size ;
23
+ private String resolution ;
24
+
25
+ public Monitor (String model , String manufacturer , int size , String resolution ) {
26
+ super (model , manufacturer );
27
+ this .size = size ;
28
+ this .resolution = resolution ;
29
+ }
30
+
31
+ public void drawPixelAt (int x , int y , String color ) {
32
+ System .out .println ("Drawing pixel at " + x + ", " + y + " in color " + color );
33
+ }
34
+
35
+ }
36
+
37
+ class Motherboard extends Product {
38
+ private int ramSlots ;
39
+ private int cardSlots ;
40
+ private String bios ;
41
+
42
+ public Motherboard (String model , String manufacturer , int ramSlots , int cardSlots , String bios ) {
43
+ super (model , manufacturer );
44
+ this .ramSlots = ramSlots ;
45
+ this .cardSlots = cardSlots ;
46
+ this .bios = bios ;
47
+ }
48
+ }
49
+
50
+ class Dimensions {
51
+ private int width ;
52
+ private int height ;
53
+ private int depth ;
54
+
55
+ public Dimensions (int width , int height , int depth ) {
56
+ this .width = width ;
57
+ this .height = height ;
58
+ this .depth = depth ;
59
+ }
60
+ }
61
+
62
+ class Case {
63
+ private String model ;
64
+ private String manufacturer ;
65
+ private String powerSupply ;
66
+ private Dimensions dimensions ;
67
+
68
+ public Case (String model , String manufacturer , String powerSupply , Dimensions dimensions ) {
69
+ this .model = model ;
70
+ this .manufacturer = manufacturer ;
71
+ this .powerSupply = powerSupply ;
72
+ this .dimensions = dimensions ;
73
+ }
74
+
75
+ public void pressPowerButton () {
76
+ System .out .println ("Power button pressed" );
77
+ }
78
+ }
79
+
80
+ class PC {
81
+ private Case theCase ;
82
+ private Monitor monitor ;
83
+ private Motherboard motherboard ;
84
+
85
+ public PC (Case theCase , Monitor monitor , Motherboard motherboard ) {
86
+ this .theCase = theCase ;
87
+ this .monitor = monitor ;
88
+ this .motherboard = motherboard ;
89
+ }
90
+ }
91
+
92
+ /**
93
+ * notes
94
+ *
95
+ * comparision in inheritance and composition
96
+ *
97
+ * Inheritance and composition are two programming techniques developers
98
+ * use to establish relationships between classes and objects.
99
+ * Whereas inheritance derives one class from another, composition
100
+ * defines a class as the sum of its parts.
101
+ *
102
+ *
103
+ * Classes and objects created through inheritance are tightly coupled b
104
+ * ecause changing the parent or superclass in an inheritance relationship
105
+ * risks breaking your code. Classes and objects created through composition
106
+ * are loosely coupled, meaning that you can more easily change the component
107
+ * parts without breaking your code.
108
+ *
109
+ *
110
+ * Because loosely coupled code offers more flexibility, many developers
111
+ * have learned that composition is a better technique than inheritance,
112
+ * but the truth is more complex. Choosing a programming tool is similar to ch
113
+ * oosing the correct kitchen tool: You wouldn't use a butter knife to cut veg
114
+ * etables, and in the same way you shouldn't choose composition for every pro
115
+ * gramming scenario.
116
+ *
117
+ *
118
+ * In this Java Challenger you'll learn the difference between inheritan
119
+ * ce and composition and how to decide which is correct for your program. Ne
120
+ * xt, I'll introduce you to several important but challenging aspects of Java
121
+ * inheritance: method overriding, the super keyword, and type casting. Finally
122
+ * , you'll test what you've learned by working through an inheritance example
123
+ * lin
124
+ * e by line to determine what the output should be.
125
+ *
126
+ *
127
+ * When to use inheritance in Java
128
+ *
129
+ * In object-oriented programming, we can use inheritance when we know t
130
+ * here is an "is a" relationship between a child and its parent class. Some exa
131
+ * mples would be:
132
+ *
133
+ *
134
+ * A person is a human.
135
+ *
136
+ * A cat is an animal.
137
+ *
138
+ * A car is a vehicle.
139
+ *
140
+ * In each case, the child or subclass is a specialized version of the p
141
+ * arent or superclass. Inheriting from the superclass is an example of code
142
+ * reuse. To better understand this relationship, take a moment to study th
143
+ * e Car class, which inherits from Vehicle:
144
+ *
145
+ *
146
+ * class Vehicle {
147
+ *
148
+ * String brand;
149
+ * String color;
150
+ * double weight;
151
+ * double speed;
152
+ *
153
+ * void move() {
154
+ * System.out.println("The vehicle is moving");
155
+ * }
156
+ * }
157
+ *
158
+ * public class Car extends Vehicle {
159
+ * String licensePlateNumber;
160
+ * String owner;
161
+ * String bodyStyle;
162
+ *
163
+ * public static void main(String... inheritanceExample) {
164
+ * System.out.println(new Vehicle().brand);
165
+ * System.out.println(new Car().brand);
166
+ * new Car().move();
167
+ * }
168
+ * }
169
+ * When you are considering using inheritance, ask yourself whether th
170
+ * e subclass really is a more specialized version of the superclass. In t
171
+ * his case, a car is a type of vehicle, so the inheritance relationship makes
172
+ * sense.
173
+ *
174
+ *
175
+ * When to use composition in Java
176
+ * In object-oriented programming, we can use composition in cases where o
177
+ * ne object "has" (or is part of) another object. Some examples would be:
178
+ *
179
+ * A car has a battery (a battery is part of a car).
180
+ * A person has a heart (a heart is part of a person).
181
+ * A house has a living room (a living room is part of a house).
182
+ * To better understand this type of relationship, consider the compo
183
+ * sition of a House:
184
+ *
185
+ *
186
+ * public class CompositionExample {
187
+ *
188
+ * public static void main(String... houseComposition) {
189
+ * new House(new Bedroom(), new LivingRoom());
190
+ * // The house now is composed with a Bedroom and a LivingRoom
191
+ * }
192
+ *
193
+ *
194
+ * static class House {
195
+ *
196
+ * Bedroom bedroom;
197
+ * LivingRoom livingRoom;
198
+ *
199
+ * House(Bedroom bedroom, LivingRoom livingRoom) {
200
+ * this.bedroom = bedroom;
201
+ * this.livingRoom = livingRoom;
202
+ * }
203
+ * }
204
+ * static class Bedroom { }
205
+ * static class LivingRoom { }
206
+ * }
207
+ * In this case, we know that a house has a living room and a bedroom, so w
208
+ * e can use the Bedroom and LivingRoom objects in the composition of a House.
209
+ */
0 commit comments