Skip to content

Commit b35cf4d

Browse files
committed
complete 08 - OOP Part 2 Polymorphism
1 parent bd7618a commit b35cf4d

File tree

18 files changed

+1705
-36
lines changed

18 files changed

+1705
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,74 @@
1-
/*
2-
Introduction
1+
/**
2+
* Introduction
3+
*
4+
* @author Chirag Singhal
5+
*/
36

7+
// we have covered inheritance and encapsulation in the previous section
8+
// now we will learn about composition and polymorphism
9+
10+
class Product {
11+
private String model;
12+
private String Manufacturer;
13+
private int width;
14+
private int height;
15+
private int depth;
16+
17+
public Product(String model, String manufacturer) {
18+
this.model = model;
19+
Manufacturer = manufacturer;
20+
}
21+
22+
}
23+
24+
class Monitor extends Product {
25+
26+
public Monitor(String model, String manufacturer) {
27+
super(model, manufacturer);
28+
}
29+
}
30+
31+
class Motherboard extends Product {
32+
33+
public Motherboard(String model, String manufacturer) {
34+
super(model, manufacturer);
35+
}
36+
}
37+
38+
public class Introduction {
39+
public static void main(String[] args) {
40+
Product product = new Product("Model", "Manufacturer");
41+
Monitor monitor = new Monitor("Model", "Manufacturer");
42+
Motherboard motherboard = new Motherboard("Model", "Manufacturer");
43+
44+
System.out.println(product);
45+
System.out.println(monitor);
46+
System.out.println(motherboard);
47+
48+
}
49+
}
50+
51+
52+
/** notes
53+
*
54+
* 1. Composition is a form of aggregation in which the composed object is a part of the whole object
55+
* 2. Inheritance is a form of aggregation in which the derived class is a part of the whole object
56+
*
57+
* 3. Composition is a "has-a" relationship
58+
* 4. Inheritance is a "is-a" relationship
59+
*
60+
* 5. Composition is used when the composed object is a part of the whole object
61+
* 6. Inheritance is used when the derived class is a part of the whole object
62+
*
63+
* 7. Composition is used when we want to reuse the class
64+
* 8. Inheritance is used when we want to reuse the behavior
65+
*
66+
* 9. Composition is used when we want to reuse the class
67+
* 10. Inheritance is used when we want to reuse the behavior
68+
*
69+
* 11. Composition is used when we want to reuse the class
70+
* 12. Inheritance is used when we want to reuse the behavior
71+
*
72+
* 13. Composition is used when we want to reuse the class
73+
* 14. Inheritance is used when we want to reuse the behavior
74+
* */
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,209 @@
1-
/*
2-
Composition Part 1
1+
/**
2+
* Composition Part 1
3+
*
4+
* @author Chirag Singhal
5+
*/
36

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+
*/
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,80 @@
1-
/*
2-
Composition Part 2
1+
/** Composition Part 2
2+
*
3+
* @author Chirag Singhal
4+
*/
35

6+
class Product {
7+
private String model;
8+
private String Manufacturer;
9+
private Dimensions dimensions;
10+
11+
public Product(String model, String manufacturer, Dimensions dimensions) {
12+
this.model = model;
13+
Manufacturer = manufacturer;
14+
this.dimensions = dimensions;
15+
}
16+
17+
}
18+
19+
class Monitor extends Product {
20+
private int size;
21+
private String resolution;
22+
23+
public Monitor(String model, String manufacturer, int size, String resolution, Dimensions dimensions) {
24+
super(model, manufacturer, dimensions);
25+
this.size = size;
26+
this.resolution = resolution;
27+
}
28+
29+
public void drawPixelAt(int x, int y, String color) {
30+
System.out.println("Drawing pixel at " + x + ", " + y + " in color " + color);
31+
}
32+
33+
}
34+
35+
class Motherboard extends Product {
36+
private int ramSlots;
37+
private int cardSlots;
38+
private String bios;
39+
40+
public Motherboard(String model, String manufacturer, int ramSlots, int cardSlots, String bios, Dimensions dimensions) {
41+
super(model, manufacturer, dimensions);
42+
this.ramSlots = ramSlots;
43+
this.cardSlots = cardSlots;
44+
this.bios = bios;
45+
}
46+
}
47+
48+
class Dimensions {
49+
private int width;
50+
private int height;
51+
private int depth;
52+
53+
public Dimensions(int width, int height, int depth) {
54+
this.width = width;
55+
this.height = height;
56+
this.depth = depth;
57+
}
58+
}
59+
60+
public class CompositionPart2 {
61+
public static void main(String[] args) {
62+
Dimensions dimensions = new Dimensions(20, 20, 5);
63+
Monitor monitor = new Monitor("27inch Beast", "Acer", 27, "1080p", dimensions);
64+
Motherboard motherboard = new Motherboard("BJ-200", "Asus", 4, 6, "v2.44", dimensions);
65+
66+
System.out.println(monitor);
67+
System.out.println(motherboard);
68+
}
69+
}
70+
71+
/** notes
72+
* 1. Composition is a form of aggregation in which the composed object is a part of the whole object
73+
* 2. Inheritance is a form of aggregation in which the derived class is a part of the whole object
74+
*
75+
* 3. Composition is a "has-a" relationship
76+
* 4. Inheritance is a "is-a" relationship
77+
*
78+
* 5. Composition is used when the composed object is a part of the whole object
79+
* 6. Inheritance is used when the derived class is a part of the whole object
80+
*/
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
/*
2-
Composition Challenge
3-
1+
/** Composition Challenge
2+
*
3+
* @author Chirag Singhal
4+
*/

0 commit comments

Comments
 (0)