KaiquanMah commited on
Commit
8e7d332
·
verified ·
1 Parent(s): 290ecd8

reusable method for SUPERCLASS n SUBCLASSES, ArrayList, SUPERCLASS METHOD

Browse files
Week 6: Methods of OO Programming/09 Polymorphism+++ ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Polymorphism may sound like a complex term, but in practice, it is a rather simple concept of object-oriented programming.
2
+ It means that the SAME OBJECT can be REFERENCED BY using DIFFERENT TYPE VARIABLES.
3
+
4
+ Since ALL CLASSES in Java INHERIT (directly or indirectly) class 'Object', an 'Object' type variable CAN REFERENCE ANY OBJECT in Java:
5
+
6
+ public static void main(String[] args) {
7
+ Object string = "Hi all!";
8
+ Object student = new Student("1234", "Sarah Student", 4);
9
+ Object list = new ArrayList<Integer>();
10
+ }
11
+
12
+
13
+
14
+ ================================================================
15
+
16
+ Then why wont we use 'Object' type variables only?
17
+ The 'TYPE' of the VARIABLE DEFINES WHAT MEMBERS CAN BE REFERENCED using that variable.
18
+ Since there is only a handful of methods defined in the class 'Object' (for example 'toString' and 'equals'),
19
+ we can only REFERENCE THOSE METHODS when the type of the variable is Object.
20
+
21
+
22
+ public static void main(String[] args) {
23
+ Object string = "Hi all!";
24
+ Object student = new Student("1234", "Sarah Student", 4);
25
+ Object list = new ArrayList<Integer>();
26
+
27
+ System.out.println(string.toString());
28
+ System.out.println(student.toString());
29
+ System.out.println(list.toString());
30
+ }
31
+
32
+ Program output
33
+ Hi all!
34
+ Sarah Student (1234), 4 op.
35
+ []
36
+
37
+
38
+
39
+
40
+
41
+ Even if your Student/Opiskelija class/object has other methods,
42
+ trying to reference ur other/custom methods provides an error message,
43
+ if the members are not declared in the 'variable type class':
44
+ ******************************************
45
+ => WHAT DOES THIS MEAN???????
46
+ *******TO CLARIFY**************
47
+ => members (of a class) = 1. variables/fields, 2. methods/fns
48
+ => members not declared => variable, method not declared in the 'reference type' class (i.e. the 'Object' class over here)
49
+ => 'variable type class' = class/type for a variable
50
+ eg
51
+ Animal animal = new Dog();
52
+ Animal = class/type = 'variable type class'
53
+ animal = variable
54
+ compiler knows 'animal' is an Animal object
55
+ compiler does not know 'animal' is a Dog object
56
+
57
+ ******************************************
58
+
59
+
60
+ Object student = new Opiskelija("1234", "Sarah Student", 4);
61
+ // getName method is not defined in 'Object' class
62
+ // even if actual object of 'Student/Opiskelija' class contains the 'getName' method => we still get compiler error
63
+ String name = student.getName();
64
+
65
+
66
+ Program produces a compiler error
67
+ The method getName() is 'undefined' for the type Object
68
+
69
+
70
+
71
+
72
+ ================================================================
73
+
74
+
75
+
76
+ Why Polymorphism?
77
+ Since an object can be REFERENCED BY its OWN TYPE variables and by ALL SUPERTYPE VARIABLES,
78
+ we can for example write a method that works on several different 'type objects'.
79
+ ******************************************
80
+ => WHAT DOES THIS MEAN???????
81
+ reference type vs actual object type
82
+ eg
83
+ class Animal { }
84
+ class Dog extends Animal { }
85
+ // => dog is of type Dog, so you can call all methods defined in Dog.
86
+ Dog dog = new Dog(); // Own type
87
+
88
+ // => animal is of type Animal, but holds a Dog. You can only access methods declared in Animal.
89
+ Animal animal = new Dog(); // Supertype reference
90
+
91
+ // => obj is of type Object, but holds a Dog. Again, limited to Object methods unless cast.
92
+ Object obj = new Dog(); // Also a supertype (since Object is the root)
93
+
94
+
95
+ So the same 'Dog' object can be referenced by :
96
+ Its OWN TYPE : Dog
97
+ ANY SUPERTYPE : Animal, Object
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+ => also what does type object/s mean?
108
+ =>'type object' = 1 method work on objects of different classes, as long as related thru inheritance
109
+ => so method allows supertype OR subtypes
110
+ eg
111
+ class Animal {
112
+ void makeSound() {
113
+ System.out.println("Some sound");
114
+ }
115
+ }
116
+
117
+ class Dog extends Animal {
118
+ @Override
119
+ void makeSound() {
120
+ System.out.println("Bark!");
121
+ }
122
+ }
123
+
124
+ class Cat extends Animal {
125
+ @Override
126
+ void makeSound() {
127
+ System.out.println("Meow!");
128
+ }
129
+ }
130
+
131
+
132
+ public class Test {
133
+ public static void main(String[] args) {
134
+ // 1 ...OTHER CODE...
135
+
136
+
137
+ // 3 calling the method
138
+ //Even though the method expects an Animal, it can accept any subclass (Dog, Cat) because they are subtypes of Animal.
139
+ //polymorphism = one interface, many forms
140
+ letAnimalMakeSound(new Dog()); // Bark!
141
+ letAnimalMakeSound(new Cat()); // Meow!
142
+ letAnimalMakeSound(new Animal()); // Some sound
143
+ }
144
+
145
+
146
+ // 2 THEN DEFINE REUSABLE METHOD ACROSS CLASSES
147
+ void letAnimalMakeSound(Animal animal) {
148
+ animal.makeSound();
149
+ }
150
+ }
151
+
152
+
153
+ ******************************************
154
+
155
+
156
+
157
+ Let's consider a class hierarchy presented last week.
158
+ We have a class 'Person' which is inherited by classes 'Student' and 'Teacher'. Here are summaries of the classes:
159
+
160
+ class Person {
161
+ private String name;
162
+ private String email;
163
+
164
+ public Person(String name, String email) {
165
+ this.name = name;
166
+ this.email = email;
167
+ }
168
+
169
+ public String getName() {
170
+ return name;
171
+ }
172
+
173
+ public String getEmail() {
174
+ return email;
175
+ }
176
+ }
177
+
178
+
179
+
180
+ class Teacher extends Person {
181
+ private int hours;
182
+
183
+ public Teacher(String name, String email, int hours) {
184
+ super(name, email);
185
+ this.hours = hours;
186
+ }
187
+
188
+ public int getHours() {
189
+ return hours;
190
+ }
191
+ }
192
+
193
+
194
+
195
+ class Student extends Person {
196
+ private int credits;
197
+
198
+ public Student(String name, String email, int credits) {
199
+ super(name, email);
200
+ this.credits = credits;
201
+ }
202
+
203
+ public int getCredits() {
204
+ return credits;
205
+ }
206
+ }
207
+
208
+
209
+
210
+
211
+ THEN
212
+
213
+
214
+
215
+ Now we can write a method that receives a 'Person' type object as an ARGUMENT.
216
+ The method outputs the email address of the person.
217
+ => 'outputEmail method' below
218
+
219
+ In addition to 'Person' type objects, the method can receive 'Student' and 'Teacher' type objects as ARGUMENTS.
220
+ This is due to the fact that students and teachers are also persons in our class hierarchy.
221
+
222
+ public class Test {
223
+ public static void main(String[] args) {
224
+ Person person = new Person("Eric Example", "eric@example.com");
225
+ outputEmail(person);
226
+
227
+ Teacher teacher = new Teacher("Tim Teacher", "tim@example.com", 25);
228
+ outputEmail(teacher);
229
+
230
+ Student student = new Student("Sarah Student", "sarah@example.com", 241);
231
+ outputEmail(student);
232
+ }
233
+
234
+ // DEFINE REUSABLE METHOD ACROSS CLASSES HERE
235
+ public static void outputEmail(Person person) {
236
+ System.out.println(person.getEmail());
237
+ }
238
+ }
239
+
240
+ Program output
241
+ eric@example.com
242
+ tim@example.com
243
+ sarah@example.com
244
+
245
+
246
+
247
+ VS
248
+
249
+
250
+ Similarly, we could create a 'Person' type list and save 'Person, Teacher and Student' objects into it:
251
+
252
+ Person person = new Person("Eric Example", "eric@example.com");
253
+ Teacher teacher = new Teacher("Tim Teacher", "tim@example.com", 25);
254
+ Student student = new Student("Sarah Student", "sarah@example.com", 241);
255
+
256
+ ArrayList<Person> persons = new ArrayList<>();
257
+ persons.add(person));
258
+ persons.add(teacher);
259
+ persons.add(student);
260
+
261
+
262
+
263
+
264
+ VS
265
+
266
+
267
+
268
+ Now we can iterate the list and call the method 'getName' for all objects.
269
+ Again, it does not matter if the type of the object is Person, Teacher, or Student;
270
+ they all have the getName method.
271
+ => BECAUSE 'getName' WAS DEFINED IN THE PARENT CLASS 'Person'
272
+
273
+ for (Person p : persons) {
274
+ System.out.println(p.getName());
275
+ }
276
+
277
+
278
+ Program output
279
+ Eric Example
280
+ Tim Teacher
281
+ Sarah Student
282
+
283
+
284
+
285
+