File size: 7,309 Bytes
8e7d332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
Polymorphism may sound like a complex term, but in practice, it is a rather simple concept of object-oriented programming. 
It means that the SAME OBJECT can be REFERENCED BY using DIFFERENT TYPE VARIABLES.

Since ALL CLASSES in Java INHERIT (directly or indirectly) class 'Object', an 'Object' type variable CAN REFERENCE ANY OBJECT in Java:

public static void main(String[] args) {
    Object string = "Hi all!";
    Object student = new Student("1234", "Sarah Student", 4);
    Object list = new ArrayList<Integer>();
}
 


================================================================

Then why wont we use 'Object' type variables only?
The 'TYPE' of the VARIABLE DEFINES WHAT MEMBERS CAN BE REFERENCED using that variable. 
Since there is only a handful of methods defined in the class 'Object' (for example 'toString' and 'equals'), 
we can only REFERENCE THOSE METHODS when the type of the variable is Object.


public static void main(String[] args) { 
    Object string = "Hi all!"; 
    Object student = new Student("1234", "Sarah Student", 4); 
    Object list = new ArrayList<Integer>();
    
    System.out.println(string.toString());
    System.out.println(student.toString());
    System.out.println(list.toString());
}

Program output
Hi all!
Sarah Student (1234), 4 op.
[]



 

Even if your Student/Opiskelija class/object has other methods, 
trying to reference ur other/custom methods provides an error message, 
if the members are not declared in the 'variable type class':
******************************************
=> WHAT DOES THIS MEAN???????
*******TO CLARIFY**************
=> members (of a class) = 1. variables/fields, 2. methods/fns
=> members not declared => variable, method not declared in the 'reference type' class (i.e. the 'Object' class over here)
=> 'variable type class' = class/type for a variable
eg
Animal animal = new Dog();
Animal = class/type = 'variable type class'
animal = variable
compiler knows 'animal' is an Animal object
compiler does not know 'animal' is a Dog object

******************************************


Object student = new Opiskelija("1234", "Sarah Student", 4);
// getName method is not defined in 'Object' class
// even if actual object of 'Student/Opiskelija' class contains the 'getName' method => we still get compiler error
String name = student.getName(); 

 
Program produces a compiler error
The method getName() is 'undefined' for the type Object




================================================================



Why Polymorphism?
Since an object can be REFERENCED BY its OWN TYPE variables and by ALL SUPERTYPE VARIABLES, 
we can for example write a method that works on several different 'type objects'.
******************************************
=> WHAT DOES THIS MEAN???????
reference type vs actual object type
eg
class Animal { }
class Dog extends Animal { }
// => dog is of type Dog, so you can call all methods defined in Dog.
Dog dog = new Dog();      // Own type

// => animal is of type Animal, but holds a Dog. You can only access methods declared in Animal.
Animal animal = new Dog(); // Supertype reference

// => obj is of type Object, but holds a Dog. Again, limited to Object methods unless cast.
Object obj = new Dog();   // Also a supertype (since Object is the root)


So the same 'Dog' object can be referenced by :
Its OWN TYPE : Dog
ANY SUPERTYPE : Animal, Object









=> also what does type object/s mean?
=>'type object' = 1 method work on objects of different classes, as long as related thru inheritance
=> so method allows supertype OR subtypes
eg
class Animal {
    void makeSound() {
        System.out.println("Some sound");
    }
}

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Bark!");
    }
}

class Cat extends Animal {
    @Override
    void makeSound() {
        System.out.println("Meow!");
    }
}


public class Test {
    public static void main(String[] args) {
    // 1 ...OTHER CODE...

    
    // 3 calling the method
    //Even though the method expects an Animal, it can accept any subclass (Dog, Cat) because they are subtypes of Animal.
    //polymorphism = one interface, many forms
    letAnimalMakeSound(new Dog());   // Bark!
    letAnimalMakeSound(new Cat());   // Meow!
    letAnimalMakeSound(new Animal()); // Some sound
    }

    
    // 2 THEN DEFINE REUSABLE METHOD ACROSS CLASSES
    void letAnimalMakeSound(Animal animal) {
        animal.makeSound();
    }
}
    

******************************************



Let's consider a class hierarchy presented last week. 
We have a class 'Person' which is inherited by classes 'Student' and 'Teacher'. Here are summaries of the classes:

class Person {
    private String name;
    private String email;

    public Person(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }
}



class Teacher extends Person {
    private int hours;

    public Teacher(String name, String email, int hours) {
        super(name, email);
        this.hours = hours;
    }

    public int getHours() {
        return hours;
    } 
}



class Student extends Person {
    private int credits;

    public Student(String name, String email, int credits) {
        super(name, email);
        this.credits = credits;
    }

    public int getCredits() {
        return credits;
    } 
}
 



THEN



Now we can write a method that receives a 'Person' type object as an ARGUMENT. 
The method outputs the email address of the person.
=> 'outputEmail method' below

In addition to 'Person' type objects, the method can receive 'Student' and 'Teacher' type objects as ARGUMENTS. 
This is due to the fact that students and teachers are also persons in our class hierarchy.

public class Test {
    public static void main(String[] args) {
       Person person = new Person("Eric Example", "eric@example.com");
       outputEmail(person);
       
       Teacher teacher = new Teacher("Tim Teacher", "tim@example.com", 25);
       outputEmail(teacher);
       
       Student student = new Student("Sarah Student", "sarah@example.com", 241);
       outputEmail(student);
    }

    // DEFINE REUSABLE METHOD ACROSS CLASSES HERE
    public static void outputEmail(Person person) {
        System.out.println(person.getEmail());
    }
}

Program output
eric@example.com
tim@example.com
sarah@example.com



 VS
 

Similarly, we could create a 'Person' type list and save 'Person, Teacher and Student' objects into it:

Person person = new Person("Eric Example", "eric@example.com");
Teacher teacher = new Teacher("Tim Teacher", "tim@example.com", 25);
Student student = new Student("Sarah Student", "sarah@example.com", 241);
  
ArrayList<Person> persons = new ArrayList<>();
persons.add(person));
persons.add(teacher);
persons.add(student);
 



VS



Now we can iterate the list and call the method 'getName' for all objects. 
Again, it does not matter if the type of the object is Person, Teacher, or Student; 
they all have the getName method.
=> BECAUSE 'getName' WAS DEFINED IN THE PARENT CLASS 'Person'

for (Person p : persons) {
    System.out.println(p.getName());
}
 

Program output
Eric Example
Tim Teacher
Sarah Student